Home > OS >  Address-of operator in C
Address-of operator in C

Time:12-22

In C programming, does the address-of operator & result in object's first address?

For example:

int a[2] = {10, 20};   
int* arrays_first_address = &a;

As &a means "array's first address" then can I generalize it so the address-of operator results in object's first address?

CodePudding user response:

int a[2];

If you use &a it will give you address of a[0].

&a = &a[0]

array name is pointer to its 0th element, that is base address.

Then you can use base address to get address of other locations. such as &a[1] = (a 1).

CodePudding user response:

I suggest to run this program :

    int a[2] = {10, 20};
    
    printf("a=%p\n", a);
    printf("&a=%p\n", &a);
    printf("&(a[0])=%p\n", &(a[0]) );

And you should have answer.

In C, an array is also see as a pointer, but there is no memory really alloc with this value. So it's a convention to says that "a" and "&a" have the same value.

  •  Tags:  
  • c
  • Related