C Tutorial/Array/Array Pointer
Содержание
- 1 Accessing an array using pointers
- 2 Address of second element in array (value of arraypointer+1)
- 3 Arrays and pointers: get address of an array
- 4 Arrays and pointers: get array value through array pointer
- 5 Deal with array pointer of long integer
- 6 Move array pointer to the next element
- 7 The name of an array is the same as &array[ 0 ]
Accessing an array using pointers
The array name is the pointer constant.
<source lang="cpp">#include <stdio.h>
main(){
int a[5]; int i; for(i = 0;i<5;i++){ a[i]=i; } int *b; b=a; for(i = 0;i<5;i++){ printf("value in array %d and address is %16lu\n",*b,b); b=b+2; }
}</source>
value in array 0 and address is 631652 value in array 2 and address is 631660 value in array 4 and address is 631668 value in array 631676 and address is 631676 value in array 42920 and address is 631684
Address of second element in array (value of arraypointer+1)
<source lang="cpp">#include <stdio.h> int main(void) {
char multiple[] = "another string"; printf("\nAddress of second element: %p", &multiple[1]); printf("\nValue of multiple+1 : %p\n", multiple+1); return 0;
}</source>
Address of second element: 9a372 Value of multiple+1 : 9a372
Arrays and pointers: get address of an array
<source lang="cpp">#include <stdio.h> int main(void) {
char multiple[] = "My string"; char *p = &multiple[0]; printf("\nThe address of the first array element : %p", p); p = multiple; printf("\nThe address obtained from the array name: %p\n", p); return 0;
}</source>
The address of the first array element : 9a372 The address obtained from the array name: 9a372
Arrays and pointers: get array value through array pointer
<source lang="cpp">#include <stdio.h> int main(void) {
char multiple[] = "a string"; char *p = multiple; int i; for(i = 0 ; i<strlen(multiple) ; i++) printf("\nmultiple[%d] = %c *(p+%d) = %c &multiple[%d] = %p p+%d = %p", i, multiple[i], i, *(p+i), i, &multiple[i], i, p+i); return 0;
}</source>
The address of the first array element : 9a372 The address obtained from the array name: 9a372
Deal with array pointer of long integer
<source lang="cpp">#include <stdio.h> int main(void) {
long multiple[] = {15L, 25L, 35L, 45L}; long * p = multiple; int i; for(i = 0 ; i<sizeof(multiple)/sizeof(multiple[0]) ; i++) printf("\naddress p+%d (&multiple[%d]): %d *(p+%d) value: %d", i,i, p+i,i,*(p+i)); printf("\n Type long occupies: %d bytes\n", sizeof(long)); return 0;
}</source>
address p+0 (&multiple[0]): 631656 *(p+0) value: 15 address p+1 (&multiple[1]): 631660 *(p+1) value: 25 address p+2 (&multiple[2]): 631664 *(p+2) value: 35 address p+3 (&multiple[3]): 631668 *(p+3) value: 45 Type long occupies: 4 bytes
Move array pointer to the next element
<source lang="cpp">#include <stdio.h> int main(void) {
char multiple[] = "another string"; printf(" first element: %p\n", multiple); printf("second element: %p\n", multiple + 1); printf(" third element: %p\n", multiple + 2); return 0;
}</source>
first element: 9a371 second element: 9a372 third element: 9a373
The name of an array is the same as &array[ 0 ]
<source lang="cpp">#include <stdio.h> int main() {
char array[ 5 ]; printf( " array = %p\n&array[0] = %p\n" " &array = %p\n", array, &array[ 0 ], &array ); return 0;
}</source>
array = 9a37b &array[0] = 9a37b &array = 9a37b