Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

referenced array == array

could you tell my why the value of a referenced array and the value of the array itself has the same value?

i know a is a type of int* but with &a it should be int** or am i wrong?? so the value should be a pointer to the a int pointer. example code:

    #include &ltstdio.h>

    int main()
    {
        int a[10];
        printf("a without ref.: 0x%x\n",a);
        printf("a with    ref.: 0x%x\n",&a);
        return 0;
    }

http://ideone.com/KClQJ

like image 745
phschoen Avatar asked May 27 '26 10:05

phschoen


2 Answers

Name of the array decays to an pointer to its first element in this case.

like image 177
Alok Save Avatar answered May 30 '26 03:05

Alok Save


Name of the array will implicit convert to a pointer ,except for two situation ,the one situations is "&array",the other is "sizeof(array)".In both cases,name of the array is a array ,not a pointer .

For example:

int a[10];
int *p;
p = a;        //a is a pointer
p = &a;       //a is a array,&a is a constant pointer
sizeof(a);    //a is array
like image 35
coco Avatar answered May 30 '26 02:05

coco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!