Page 10 - Sample_test_Float
P. 10
10 Functions and Pointers
Program
//Program to find the sum of six numbers with arrays and pointers.
#include<stdio.h>
void main()
{
int i,class[6],sum=0;
printf("Enter 6 numbers:\n");
for(i=0;i<6;++i)
{
scanf("%d",(class+i));// (class+i) is equivalent to &class[i]
sum +=*(class+i);// *(class+i) is equivalent to class[i]
}
printf("Sum=%d",sum);
}
Output
Enter 6 numbers:
2
3
4
5
3
4
Sum = 21
Example 2
int a[5]={10,20,30,40,50};
Variable a[0] a[1] a[2] a[3] a[4]
Value 10 20 30 40 50
Address 1010 1012 1014 1016 1018
Here a[0] is the starting element and the address of a[0] is called the base address
Value of a[0] = 10.
Value of a[1] = 20.
Value of a[2] = 30.
Value of a[3] = 40.
Value of a[4] = 50.
Then Value of a =?
Now what will the variable ‘a’ hold and what will happen when the value of ‘a’ is printed?
Consider the statement below:
printf(“%d”,a);
The output of the statement would be “1010” since an array variable will hold the base address;
Now when we declare a variable as
int a[5];

