0

suppose i have something like this :

#include<stdio.h>
int main()
{
    int arr[2][2][2]={1,2,3,4,5,6,7,8};
    printf("%d\n",sizeof(int));
    printf("%d\n",&arr[0][1]);
    printf("%d\n",&arr[0][1]+1);
    printf("%d\n",arr[0][1]);
    printf("%d\n",arr[0][1]+1);
    printf("%d\n",arr[0]);  
    printf("%d\n",arr[0]+1);
    printf("%d\n",&arr[0]);
    printf("%d\n",&arr[0]+1);
    printf("%d\n",&arr);
    printf("%d\n",&arr+1);
    printf("%d\n",arr);
    printf("%d\n",arr+1);

    return 0;
}

Now when we execute it , the results of printf("%d\n",arr) ,printf("%d",arr+1) and printf("%d\n",&arr[0]), printf("%d\n",&arr[0]+1) are same.

Can someone please explain in a simple layman language what exactly is the difference when using & operator and when not using it. I mean we could only use one way , what is the need for another ??

What's the difference between array and &array?

considering this link,it has been said :

"The type of &array is int ()[16] (a pointer to an array of 16 integers). The type of array, when left to decay, is int (a pointer to an integer)."

how do you exactly determine types of &array and array,and also what is the meaning of "when left to decay" ??

Community
  • 1
  • 1
  • 1
    "*we could only use one way*" yes, to (try to) take the address of "what follows", but "*what is the need for another*": which "*another*"? – alk Apr 16 '17 at 12:55
  • 2
    You have undefined behavior for not using the right format specifiers. `%d` expects an `int`, not a pointer. – Spikatrix Apr 16 '17 at 12:55
  • `&arr[0] == &*(arr + 0) == (arr + 0) == arr` – Spikatrix Apr 16 '17 at 12:57
  • dear alk,i wanted to say that if we have &arr[0]+1 and arr+1 giving the same result then what is the significance of each ?? – Anuvansh Kumar Apr 16 '17 at 13:12
  • dear cool guy,sorry that should be %p. – Anuvansh Kumar Apr 16 '17 at 13:18
  • 1
    Doing `&arr` evalutates to a `int(*)[2][2][2]`, where as doing just `arr` evaluates to `int(*)[2][2]`, Both expressions point to the *same* memory address, but are of *different* type. – alk Apr 16 '17 at 13:26

0 Answers0