1

Consider the following program.

#include <stdio.h>

int main()
{
  int a[10]={0};

  printf("%p %p\n", a, &a);
  printf("%d %d\n", *a, *(&a));

  return 0;
}

a and &a are same. But *a and *(&a) are not. I am out of answers. Please help.

Shiv
  • 1,912
  • 1
  • 15
  • 21
  • possible duplicate of [C: How come an array's address is equal to its value?](http://stackoverflow.com/questions/2528318/c-how-come-an-arrays-address-is-equal-to-its-value) – ThiefMaster May 09 '12 at 11:47

4 Answers4

3

a[i] is the same as *(a + i). So the first element is at the same address as the array itself.

You'll find a more detailed explanation in another question here on SO: How come an array's address is equal to its value in C?

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

The address of an array is one of those things you learn early on in C. The address of the buffer is the address of its first element. In the case of

printf("%d %d\n", *a, *(&a));

*a takes the value of a.

*(&a) first take's a's address, and then deferences from that.

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

char szBuffer[100];
char *pBufferPtr = NULL;

int main(int argc, char *argv[])
{
    pBufferPtr = szBuffer;

    printf("%s %i\n %s %i\n %s %i\n", 
            "szBuffer's address: ", (int ) szBuffer,
            "&szBuffer[0]'s address: ", (int ) &szBuffer[0],
            "pBufferPtr's address: ", (int ) pBufferPtr);



    printf("\n%s\n", "Program ended.");

    return 0;
}

As I learned many years ago, sometimes the best thing to do with pointers is write what used to be called "stub" code, and then examine in the debugger or use "print" (printf) statements. What gets trickier is when you want to modify what a pointer points to, instead of its contents.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
1

There is no "array base pointer", i.e. there no pointer variable that points to the array. The name of the array refers to the array itself. Therefore you can not take an address of a, instead, &a is handled as a special case.

When you use the name of an array in an expression, it decays into a pointer that points to the first element of the array. However, & and sizeof operators are exceptions.

ANSI C specifies that &a means the address of the array itself, and its type is "pointer to array", not "pointer to array element".

In pre-ANSI compilers, &a would cause a warning.

PauliL
  • 1,259
  • 9
  • 7
  • Thanks for pointing about ANSI C that & means the address of the array itself. However, it is counter-intuitive at best. – Shiv May 09 '12 at 13:53
0

a and &a have the same value, but they are different types: int * and int **. You should have got a warning when you compiled you code.

Your second line will work if you add an extra * like this:

printf("%d %d\n", *a, **(&a));

This does make sense: if *a dereferences a and gives the first element of the array, and &a means take the address of a, then *(&a) takes the address of a, dereferences that, and arrives back at a once more.

ams
  • 24,923
  • 4
  • 54
  • 75
  • `&a` has type `int (*)[10]` and not `int **`. – ouah May 09 '12 at 12:07
  • @ouah: true, but it's the same difference, and I tried to use the one that looked cleaner. I should have put a footnote in, or something. – ams May 09 '12 at 15:04
  • The question was not about type but why a and &a report same memory location. This is plain absurd. As pointed by PauliL ANSI C they are same. This is against common sense. I am not sure about the reason behind a and &a reporting same memory location other than this. All I can guess is it will save 4 bytes on stack. However, if I would have written ANSI C standard I would not have done so. – Shiv May 09 '12 at 15:35
  • Absurd or not, it *is* about the type. It is the type that determines how an operator, such as `&`, behaves. As @Paul has said, the addressof operator doesn't apply cleanly to arrays, and thinking of arrays as pointers is misleading. – ams May 09 '12 at 16:37