0

In the below C Program, all the below 4 printf give me the same address value. I understand the first 3 quite well. I am not able to comprehend why &myintarr should be same as myintarr or &myintarr[0].

I request explanation on this aspect. Thanks.

int main()
{
    int myintarr[] = {2,4,6,8,10};
    int *iptr = myintarr;

    printf("Addrss of myintarr variable: %p\n", &myintarr[0]);
    printf("Addrss of myintarr variable: %p\n", myintarr);
    printf("Addrss of myintarr variable: %p\n", iptr);
    printf("Addrss of myintarr variable: %p\n", &myintarr); // help here
    return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
vin
  • 9
  • 2
  • `&myintarr` is the same as `myintarr`, in that case the `&` is simple superfluous. – Jabberwocky Apr 06 '16 at 17:35
  • C is intriniscally unable to handle arrays, and pointers to them, in a sane manner, consistent with the way other var types are handled. – Martin James Apr 06 '16 at 17:36
  • 1
    `&myintarr` is not the same as `myintarr`, but it pretty much usually has the same representation since they both point to the beginning of the array. `&myintattr` has type of `pointer to array of 5 ints`, whereas `myintarr` is `pointer to int` – Antti Haapala -- Слава Україні Apr 06 '16 at 17:39
  • Hi Walz, if myintarr were to be a simple integer variable rather than an array, then &myintarr and myintarr would be different, isn't it. – vin Apr 06 '16 at 17:40
  • @vin correct: the first would produce the address of the integer variable, and the second just the value of that variable. – Antti Haapala -- Слава Україні Apr 06 '16 at 17:42
  • 1
    @Michael Walz `&myintarr` is _not_ the same as `myintarr`. `&myintarr` has type _pointer to array 5 of int_ and `myintarr` has type _array 5 of int_. They also have different sizes. – chux - Reinstate Monica Apr 06 '16 at 17:45
  • @AnttiHaapala, in that case i am not able to visualize the memory for &myintarr, in case when myintarr is an array of integers, as i have given in my example. I was expecting "Suspicious Pointer Error" for my above code, but it went through even with -ansi enabled. – vin Apr 06 '16 at 17:46
  • @JohnBollinger, Yes. Thanks for the Link and the information. – vin Apr 06 '16 at 17:49
  • 1
    Summary: When an array-valued expression appears in most C contexts, it "decays" to a pointer to the first element. This is why `&myintarr[0]` and `myintarr` have the same value (and type). But the first element of an array appears at the very beginning of the array. It has the same (start) address as the whole array. That's why `myintarr` and `&myintarr` have the same value (in an approximate sense that ignores that they have different types). – John Bollinger Apr 06 '16 at 17:49
  • Note that C standard definitely does not require that `myintarray` and `&myintarray` should print the same value - on the contrary, they could print different values. (furthermore you'd have to cast them to `(void*)` for printing.) It is just true of the common implementations. – Antti Haapala -- Слава Україні Apr 06 '16 at 17:51

0 Answers0