How to use pointer to an array of N integers in C

2 Answers

0 votes
#include <stdio.h>

#define N 3

int main(void)
{
    int a[N] = {9, 5, 2};
    int b[N] = {7, 8, 1};
    
    int (*p)[N] = &a; 
    
    printf("%d\n", *p[0]); 
    printf("%d\n", *p[1]); // next array in memeory...
    
    for (int i = 0; i < N; ++i) {
        printf("a[%d] = %d\n", i, (*p)[i]);
    }
}
  
  
  
/*
run:
  
9
7
a[0] = 9
a[1] = 5
a[2] = 2
  
*/

 



answered Mar 9, 2024 by avibootz
0 votes
#include <stdio.h>

#define N 3

int main(void)
{
    int a[N] = {9, 5, 2};
    int b[N] = {7, 8, 1};
    
    int (*p)[N] = &a; 

    printf("%d\n", *(p[0]));
    printf("%d\n\n", *(p[0] + 1));
     
    for (int i = 0; i < N; i++) {
        printf("%d ", *(p[0] + i));
    }
}
  
  
  
  
/*
run:
  
9
5

9 5 2 

  
*/

 



answered Mar 9, 2024 by avibootz

Related questions

...