-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer-array.c
More file actions
40 lines (27 loc) · 1017 Bytes
/
Copy pathpointer-array.c
File metadata and controls
40 lines (27 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Pointer to an Array in C
//double balance[50];
//balance is a pointer to &balance[0], which is the address of the first element of the array balance. Thus, the following program fragment assigns p as the address of the first element of balance
double *p;
double balance[10];
p = balance;
//*(balance + 4) is a legitimate way of accessing the data at balance[4].
//Once you store the address of the first element in 'p', you can access the array elements using *p, *(p+1), *(p+2) and so on.
//Example
#include <stdio.h>
int main () {
/* an array with 5 elements */
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
int i;
p = balance;
/* output each array element's value */
printf( "Array values using pointer\n");
for ( i = 0; i < 5; i++ ) {
printf("*(p + %d) : %f\n", i, *(p + i) );
}
printf( "Array values using balance as address\n");
for ( i = 0; i < 5; i++ ) {
printf("*(balance + %d) : %f\n", i, *(balance + i) );
}
return 0;
}