Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

POINTERS IN ONE-DIMENSIONAL ARRAY

Posted By: Ramond Fischer     Category: C Programming     Views: 6513

Write a program using pointers to compute the sum of all elements stored in an array.

Code for POINTERS IN ONE-DIMENSIONAL ARRAY in C Programming

   main()                                                           
   {
       int *p, sum, i;                                              
       int x[5] = {5,9,6,3,7};                               
       i  = 0;                                                      
       p  = x;          /* initializing with base address of x */
printf("Element Value Address\n\n"); while(i < 5) { printf(" x[%d] %d %u\n", i, *p, p); sum = sum + *p; /* accessing array element */
i++, p++; /* incrementing pointer */
} printf("\n Sum = %d\n", sum); printf("\n &x[0] = %u\n", &x[0]); printf("\n p = %u\n", p); } Output Element Value Address x[0] 5 166 x[1] 9 168 x[2] 6 170 x[3] 3 172 x[4] 7 174 Sum = 55 &x[0] = 166 p = 176
  
Share: 


Didn't find what you were looking for? Find more on POINTERS IN ONE-DIMENSIONAL ARRAY Or get search suggestion and latest updates.

Ramond Fischer
Ramond Fischer author of POINTERS IN ONE-DIMENSIONAL ARRAY is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!