-1
#include <stdio.h>
int main()
{
    int *a, *b,*c,**d;
    int arr[10]={0};
    a=arr;
    b=&arr;//is this true?why?
    c=&arr[0];
    d=&a;
    printf("%p,%p,%p,%p",a,b,c,d);
    return 0;
}

Does &arr something point to the first address of the array?

Is b=&arr; true?

What's the difference between arr and &arr?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
zhm
  • 107
  • 7
  • 3
    Fyi, `&arr` isn't `int*`, it is `int (*)[10]` so `b = &arr` should pop a type warning. And `&anything` returns the address of `anything`, so long as it is a legal language context to do so, with the result being a pointer-to-type, where 'type' is the type of object being addressed. The *values* of the address in all three contexts may be the same, but the *type* can be different, and if you think it doesn't matter, google *pointer arithmetic*. – WhozCraig Jul 23 '22 at 03:24
  • 1
    The address of an array is the address of the first element. So while `arr` and `&arr` return the same address, as @WhozCraig explains, they are different in type. When referenced, an array is converted to pointer to type with the address of the first element [C18 Standard - 6.3.2.1 (3)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2596.pdf) – David C. Rankin Jul 23 '22 at 03:52
  • For an exercise, output `printf ("arr : %p\narr + 1 : %p\n&arr + 1: %p\n", (void*)arr, (void*)(arr + 1), (void*)(&arr + 1));` How far is `arr + 1` from `arr`? How far is `&arr + 1` from `arr`? – David C. Rankin Jul 23 '22 at 04:07
  • 1
    The question was editd to change from `int *b` to `int (*b)[10]` after the first answer was posted. This is NOT allowed. It wasn't the OP who changed that. The `` to `` spelling change is far less serious in most respects. – Jonathan Leffler Jul 23 '22 at 04:32
  • Does this answer your question? [How come an array's address is equal to its value in C?](https://stackoverflow.com/questions/2528318/how-come-an-arrays-address-is-equal-to-its-value-in-c) – outis Jul 23 '22 at 04:56

1 Answers1

3

Does &arr something point to the first address of the array?

&arr is pointer to whole array of 10 integers and not the pointer to first element of the array.

Is b=&arr; true?

No, given int *b, then b=&arr; is an incompatible pointer type assignment because the type of &arr is int (*)[10] whereas the type of b is int *. (If the definition is int (*b)[10], there is no problem.)

what is the difference between arr and &arr?

The arr, when used in a statement, will be converted to pointer to first element of array (there are few exceptions to this rule). The type of arr is int * and it is equivalent to &arr[0]1) (i.e. both are pointer to first element of array).

The &arr is pointer to the whole array (it's type is int (*)[10]).

The address of an array (&arr) and address of first element of an array (arr or &arr[0]) is numerically same though their type is different. That means, if we add 1 to them there results will be different (provided that the array size is greater than 1). arr + 1 result in pointer to 2nd element whereas &arr + 1 result in pointer after 10 elements of array arr.

Demonstration:

#include<stdio.h>

int main (void) {
    int arr[10];

    printf ("arr - %p\n", (void *)arr);
    printf ("&arr - %p\n", (void *)&arr);

    printf ("arr + 1 - %p\n", (void *)(arr + 1));
    printf ("&arr + 1 - %p\n", (void *)(&arr + 1));

    return 0;
}

Output:

# ./a.out
arr - 0x7ff7bec77950
&arr - 0x7ff7bec77950
arr + 1 - 0x7ff7bec77954
&arr + 1 - 0x7ff7bec77978

Also, when using the format specifier %p in printf(), the corresponding argument('s) should be type casted to (void *).


1). From C11 Standards#6.5.2.1

The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2)))..

Hence,

arr -> (arr + 0) -> &( *(arr + 0) ) -> &arr[0]
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
H.S.
  • 11,654
  • 2
  • 15
  • 32