Does an Array variable point to itself?

前端 未结 4 1818
猫巷女王i
猫巷女王i 2021-01-23 11:14

I tried some code to check the behavior of array and pointers. Its as follows.

#include 
main(){
int s[]={1,2};
int *b=s;
printf(\"%d, %d, %d\\n\"         


        
4条回答
  •  孤城傲影
    2021-01-23 12:08

    Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be replaced with an expression of type "pointer to T", and the value of the expression will be the address of the first element in the array.

    Assume the following code:

    int arr[10];
    foo(arr);
    

    In the call to the function foo, the expression arr is converted from type "10-element array of int" to "pointer to int", and the address of the first element of arr is what actually gets passed to foo.

    We would define foo as either

    void foo(int a[]) {}
    

    or

    void foo(int *a) {}
    

    In the context of a function parameter declaration, T a[] and T a[N] are identical to T *a; the parameter is a pointer type, not an array type. Note that this is only true for function parameter declarations.

    As mentioned above, one exception to this rule is when the array expression is the operand of the unary & operator. If we change the call to foo to read

    foo(&arr);
    

    then the type of the expression &arr is "pointer to 10-element array of int", or int (*)[10], and the value of the expression is the address of a. For this, the definition of foo would be

    void foo(int (*a)[10]) {}
    

    In C, the address of the array and the address of the first element of the array are the same - thus both of the expressions arr and &arr have the same value, but their types are different. This matters for operations involving pointer arithmetic. For example, assume our code had been written

    int arr[10];
    foo(arr);
    ...
    void foo(int *a)
    {
       ...
       a++;  
       ...
    }
    

    On entry, a points to arr[0]. The expression a++ would advance the pointer to point to the next integer in the array (arr[1]).

    Now assume the code had been written as

    int arr[10];
    foo(&arr);
    ...
    void foo(int (*a)[10])
    {
      ...
      a++;
      ...
    }
    

    On entry, a still points to arr[0] (remember, the address of the array is the same as the address of the first element of the array), but this time the expression a++ will advance the pointer to point to the next 10-element array of integers; instead of advancing the pointer sizeof (int) bytes, we advance it sizeof (int[10]) bytes.

    So this is why in your printf statement you see the same values for both s and &s. You should use the %p conversion specifier to print pointer values, and it expects the corresponding argument to be type void *, so change those printf statements to

    printf("%p %p %d\n", (void *) s, (void *) &s, *s);
    printf("%p %p %d\n", (void *) b, (void *) &b, *b);
    

提交回复
热议问题