- This topic has 2 replies, 2 voices, and was last updated 11 years, 7 months ago by .
Viewing 3 posts - 1 through 3 (of 3 total)
Viewing 3 posts - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.
|
Miscellaneous › Others › Pointer Addition–Sum of elements of array with pointers
#include<stdio.h>
main()
{
int a[5],sum=0,i;
int *p,j;
for ( i =0; i<5; i++)
{
printf(“Enter number:”);
scanf(“%d”,&a);
}
// Normal Way of addition
for (i=0;i<5;i++)
{
sum= sum + a;
}
printf(“nTotal sum is=%d”,sum);
// With Pointers //
sum=0;
for (i=0;i<5;i++)
{
p=&a; // p holds the address of a
sum=sum + *p; // *p holds the contents of a
}
printf(“nTotal sum is=%d”,sum);
getch();
}
Output:
Enter number:56
Enter number:55
Enter number:88
Enter number:88
Enter number:55
Total sum is=342
Total sum is=342
Is that a query?
no its not a query. its just addition of numbers using ‘C’ Language pointers. if u still have questions regarding pointers, u can ask me,