How to access int array elements using pointers in C

3 Answers

0 votes
#include <stdio.h> 

#define LEN 6
  
int main(void)
{   
    int arr[LEN] = {3, 6, 8, 1, 9, 7};
   
    for (int i = 0; i < LEN; i++)
      printf("%2d", *(arr + i));
      
    return 0;
}
  
          
/*
run:
       
 3 6 8 1 9 7
 
*/

 



answered May 25, 2017 by avibootz
0 votes
#include <stdio.h> 

#define LEN 6
  
int main(void)
{   
    int arr[LEN] = {3, 6, 8, 1, 9, 7}, *p = arr;
   
    for (int i = 0; i < LEN; i++)
      printf("%2d", *(p++));
      
    return 0;
}
  
          
/*
run:
       
 3 6 8 1 9 7
 
*/

 



answered May 25, 2017 by avibootz
0 votes
#include <stdio.h> 

#define LEN 6
  
int main(void)
{   
    int arr[LEN] = {3, 6, 2, 1, 9, 7}, *p;
    
    p = arr;
   
    for (int i = 0; i < LEN; i++)
      printf("%2d", *(p + i));
      
    return 0;
}
  
          
/*
run:
       
 3 6 2 1 9 7
 
*/

 



answered May 25, 2017 by avibootz

Related questions

1 answer 139 views
3 answers 264 views
1 answer 214 views
1 answer 278 views
1 answer 147 views
...