difference between way a[i] and *(a+1) are handled [closed]
file 1:
int mango[100];
file 2:
extern int *mango;
...
/* some code that references mango[i] */
When both are accessed as *(mango+i)
why do we get error?
Also if char mango[5];
then array starts at location mango such that mango=&mango[0]
. So variable 'mango' co开发者_Go百科ntains address to itself or the first character of array?
If you define a variable a
such as:
char a[5];
Then you have defined an array of five (5) characters. The address of the first character, which can be accessed via a[0]
or simply *a
, is named "a
". The array is also called "a
" because the array is named after the address of its first element, as given in the array declaration by the programmer.
It is not possible to change the address of a
by assigning to a
. If you attempt to write:
a = NULL;
The error you receive should indicate that an lvalue (value which may appear left of an assignment operator) is required.
The variable a
represents an array of characters, and as-such, it is a variable that is bound to the address that holds the value for the first character of the array. When array-names are passed as arguments to functions, or used on the right-hand side of the assignment operator, they decay (i.e., are implicitly converted) into pointers to the first element of the array. Thus arrays are not pointers, but because of the implicit conversion, they can be used like pointers in many C and C++ operations.
That being said, if you are defining char a[4];
in one .c file, and then including that declaration in another .c file that has a variable called char* a
, then the C-compiler is going to complain about multiple definitions of the same variable.
精彩评论