0

Consider the following C code:

char array1[1]={1};
char array2[2]={2, 2};

void viewDataPointedToBy(char* z)
{
  printf("%i\n", *z);
}

int main(void)
{
  viewDataPointedToBy(array1);   //prints 1, as expected.
  viewDataPointedToBy(array2);   //prints 2, as expected.

  viewDataPointedToBy(&array1);  //prints 1, How??
  viewDataPointedToBy(&array2);  //prints 2, How??

  viewDataPointedToBy(&&array1); //Compilation Error: 'array1' used but not defined, Why ?

  system ("pause");
  return 0;
}

I understand the outcomes of the first two calls of viewDataPointedToBy. I need a explanation for the outcomes of following three calls.

Hesham Eraqi
  • 2,444
  • 4
  • 23
  • 45
  • In the 3rd and 4th calls, (&array1), (&array2), I get errors: _"found pointer to array 1 of char, expected pointer to char"_. I am curious, with the syntax you are showing, how these printed at all? What compiler are you using? – ryyker Oct 01 '13 at 14:53
  • gcc test.c -Wall -Wextra -std=c89 -pedantic -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -o program – Hesham Eraqi Oct 01 '13 at 17:06

4 Answers4

3

This is since in C array name and array addresses are equivalent. So when you pass &array1 it is equivalent to passing array.

You may try printing the address to confirm this behavior:

printf("%u %u\n", array1, &array1); 
Manoj Awasthi
  • 3,460
  • 2
  • 22
  • 26
  • 1
    Or `%p` for printing pointers – lurker Oct 01 '13 at 14:32
  • 1
    `%u` is unsigned integer and would work for printing addresses on 32 bit machines, `%ul` on 64 bit. I think you are right, we should use `%p`. – Manoj Awasthi Oct 01 '13 at 14:35
  • Yes, I agree. I was being a little pedantic in this case, just as a suggestion. :) – lurker Oct 01 '13 at 14:36
  • I am not sure the word _equivalent_ applies. `array1` is ***not*** equal to `&array1`. However, `array1` ***is*** equal to `&array1[0]`. I am not even sure how the source shown compiled. – ryyker Oct 01 '13 at 15:05
  • @ManojAwasthi - more pedantics - 64 bit address would require `%llu`, (not `%ul`) :) of course `%p` is always good for addresses, as both you and mbranch stated. – ryyker Oct 01 '13 at 15:20
  • Hey @ryyker, you got me to try the program out. Thanks for pointing out the mistake on format specifiers. `array1` and `&array1` indeed hold the same value i.e. the base address of array (which is also same as `&array1[0]`. – Manoj Awasthi Oct 01 '13 at 15:31
  • Your welcome. Yes, Array1 and &Array1 represent the same value (as is shown with your `printf()` statement. So maybe my statement _I am not sure the word equivalent applies_ is not altogether true. But although they may be _equivalent_ in the since of value (same location of memory), they are not the same, eg. 2+2 and 4 are equal, but not the same. Array1 and &Array1[0] can be passed as an argument of the form (char *), &Array1 cannot. (at least in my compiler.) – ryyker Oct 01 '13 at 17:30
2

As for your last error - I think it is being caused by a syntax problem. You cannot doubly reference a variable. It does not really have sense. &array is a constant. Referencing it one more time gets you nowhere. I assume the error comes from the compiler trying to recognize && as the boolean binary operator.

dragosht
  • 3,237
  • 2
  • 23
  • 32
0

array itslef is a pointer, and your requested address of address.It's not acceptable for the given array.

PersianGulf
  • 2,845
  • 6
  • 47
  • 67
0

You send the base address of the array.

(1)viewDataPointedToBy(array1)

or

viewDataPointedToBy(&array1)

in both function you send the base address of array1

therefore the result of both function are same.

(2) in the case : viewDataPointedToBy(&&array1)

you can use (&&)logical and operator. Therefore error will occur.

Dev
  • 3,410
  • 4
  • 17
  • 16
  • 1
    Regarding your statement: _you send the pointer to pointer._, && is not a pointer to pointer. it is the logical operator AND, and make no sense in the context shown. – ryyker Oct 01 '13 at 15:11