2
#include<stdio.h>
#include<stdlib.h>

int main(){
    int myChr[4][8];
    printf("%x\n",myChr);
    printf("%x\n",&myChr);
    printf("%x\n",*myChr);
    return 0;
}

After executing the above program, I get the same address as output. Do they own different value or all of them have same value? How to prove that? (*Maybe need to assume values to array, I don't know)

Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
whalesf
  • 49
  • 1
  • 8

3 Answers3

4
  • myChr is the address on the stack of your array.
  • &mychr, in this case, is probably going to give you the same value as it is the address of the pointer on the stack.
  • *myChr is the address of 1st entry of myChr[4][8] entry, which in the case is still the original address.
  • **myChar would give you the value of myChr[0][0] - in this case garbage as you have not actually assigned anything to your array.
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
2

myChar is a int [4][8].

In the printf expression:

  • myChar is of type int (*)[8] (after application of C array to pointer conversion rule from int [4][8])
  • &myChar is of type int (*) [4][8]
  • *myChar is of type int * (after application of C array to pointer conversion rule from int [4])

All the expressions have different types but they point to the same memory address, that is:

 (void *) myChar == (void *) &myChar == (void *) *myChar

Note that the valid way to print a pointer value is to use p conversion specifier and cast the pointer to void * if it is of a different type.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

Arrays are simply named extents of memory. So the address of an array is the address of the extent it occupies. At the same time it is the address of the first element of the array because it occupies the initial part of the extent.

Thus you have:

this statement

printf("%x\n",myChr);

displays the address of the first element of the array because array name is implicitly converted to pointer to its first element;

this statement

printf("%x\n",&myChr);

displays the address of the array that is the same address as above because it is the address of the allocated extent;

this statement

printf("%x\n",*myChr);

displays the same address. Why? As I have already said the name of the array is implicitly converted to pointer to its first element. The element of the array is in turn a one-dimensional array. So in expression this one-dimensional array *myChr (the first element of the original two-dimensional array) in turn is converted to pointer to its first element.

So in all three cases you display this address :)

&myChr[0][0]
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335