In C language, arrays are arrays, pointers are pointers, and the two are completely different. In general, one is an address symbol, and the other is a variable that stores the variable address.
But sometimes they can be the same.
First, we should know when the two are "similar.
1. An array in an expression is a pointer;
2. The C language uses the array subscript as the pointer offset;
3. "array name as function parameter" is equivalent to pointer;
Array references, such as a [I], are always rewritten by the compiler into the form of * (a + I) during compilation. The reason why the C language converts an array parameter to a pointer is in efficiency, because it is impossible to copy the entire array, instead, "All arrays are converted as pointers to the starting address of the array when passed as parameters ".
From Expert C Programming --- Peter van der Linden
Manual Lab 1:
[Lanux @ localhost program] $ cat pointer_and_array_01.c
# Include <stdio. h>
Char Ga [8] = "goblecha ";
Void print_array (char arr [])
{
Printf ("/Nin function print_array:/N ");
Printf ("arr address, on goble GA: % d/N", & ARR );
Printf ("arr [0] address, on goble GA: % d/N", & (ARR [0]);
Printf ("arr [1] address, on goble GA: % d/N", & (ARR [1]);
}
Void print_pointer (char * PTR)
{
Printf ("/Nin function print_pointer:/N ");
Printf ("PTR value, on goble GA: % d/N", & (* PTR ));
Printf ("PTR address, on goble GA: % d/N", & PTR );
Printf ("PTR [0] address, on goble GA: % d/N", & (PTR [0]);
Printf ("PTR [1] address, on goble GA: % d/N", & (PTR [1]);
Printf ("++ PTR value, on goble GA: % d/N", ++ PTR );
}
Main ()
{
Printf ("/Nin the main:/N ");
Printf ("goble ga adrress: % d/N", & Ga );
Printf ("goble Ga [0] address: % d/N", & (Ga [0]);
Printf ("goble Ga [1] address: % d/N", & (Ga [1]);
Print_array (GA );
Print_pointer (GA );
Printf ("/N ");
Return 0;
}
[Lanux @ localhost program] $ gcc-W pointer_and_array_01.c-o Test
[Lanux @ localhost program] $ ls
A. Out notes.1 pointer_and_array_01.c Test
[Lanux @ localhost program] $./test
In the main:
Goble ga adrress: 134519160
Goble Ga [0] address: 134519160
Goble Ga [1] address: 134519161
In function print_array:
Arr address, on goble GA:-1077022384
Arr [0] address, on goble GA: 134519160
Arr [1] address, on goble GA: 134519161
In function print_pointer:
PTR value, on goble GA: 134519160
PTR address, on goble GA:-1077022384
PTR [0] address, on goble GA: 134519160
PTR [1] address, on goble GA: 134519161
++ PTR value, on goble GA: 134519161
[Lanux @ localhost program] $
The initialized global variable GA is stored in the global zone, while the function parameter variables stored in the stack zone, such as arr [] and * PTR, are actually a pointer, that is, a variable storing the address (the address here points to the global character array Ga ).
01:26:10