1

Consider the following code snippet.

int main(){
    int x[2];
    x[0]=100;
    x[1]=200;
    printf("%d\n",x);
    printf("%d\n",&x);
    printf("%d\n",*x);
}   

The output is given as (in three lines)

6487616  6487616 100

I've read that the array name is a pointer to the first element of the array. Therefore, the value of 'x' prints a memory address. But if i try to print the address of the pointer which holds the address of the first element of the array, why does that also print the same memory address? Shouldn't it be printing another memory address, simply because in order for a variable to store a memory address it should be a pointer and that pointer variable also has a memory address in the RAM.

int main(){
   int y = 10;
   int *p = &y;

   printf("%d\n",&y);
   printf("%d\n",&p);   
}

The above code gives the output as

6487628 6487616

So why doesn't this work when it comes to arrays?

Dilini Peiris
  • 446
  • 1
  • 6
  • 16
  • 3
    You shouldn't use "%d" to print a pointer address, the behaviour on doing that is undefined. – Bathsheba Mar 13 '18 at 16:56
  • @Bathsheba "%p" gives the hexadecimal value of the memory address and "%d" gives the integer value right? Please correct me if i am wrong. And according to my knowledge, that does not affect to the question i am asking here – Dilini Peiris Mar 13 '18 at 17:00
  • 1
    `%p` is different if pointers are 64-bit and integers are 32-bit. – Jean-François Fabre Mar 13 '18 at 17:00
  • `y` and `p` are 2 separate variables. Their addresses are different. you mean to print `p` not `&p` – Jean-François Fabre Mar 13 '18 at 17:01
  • There is no such thing as *"the address of the array name"*. `&x` is the address of the array. The name of a variable is just a name for a memory location (address). The array name (`x`) is the address in memory where the array starts and, for most purposes, it is the same thing as `&x`. – axiac Mar 13 '18 at 17:01
  • @axiac you say that _ for most purposes, it is the same thing as `&x`_ . which means there are times that `x` and `&x` print different values? which is what i am concerned here – Dilini Peiris Mar 13 '18 at 17:05
  • Arrays simply hold the memory address of its first element. Whether or not the reference operator is used, it decays to a pointer to its first element. – AstroMax Mar 13 '18 at 17:10
  • 1
    @AstroMax arrays do not hold the memory address of its first element. The address is not stored anywhere. The array holds all of the elements. See the top rated answer. – Kevin Mar 13 '18 at 17:13
  • @DiliniPeiris No, this is one of the cases when `x` and `&x` are the same thing. – axiac Mar 13 '18 at 17:21
  • @Kevin Thanks for pointing out. I had it differently in my mind and you cleaned it up. – AstroMax Mar 13 '18 at 17:22
  • this is worth looking through. @DiliniPeiris see question 6.9 and 6.12 especially: http://www.c-faq.com/aryptr/index.html – yano Mar 13 '18 at 18:16

4 Answers4

5

Contrary to what you might have heard, arrays and pointers are not the same thing. An array is a sequence of one or more elements of a given type, while a pointer points to a memory location (which may be the first element of an array). Because of this, the address of an array is the same as the address of the first element.

Where the confusion comes in is that in most expressions, an array decays into a pointer to the first element. One of the times this decaying does not happen is when the array is the subject of the address-of operator &.

In your first piece of code, you first print x. Ignoring for the moment that you should be using %p to print a pointer instead of %d, here x decays into a pointer to the first element and that address is what is printed. In the next call to printf, you pass in &x. This has the same value as x (when converted to a pointer) but has a different type, i.e. x has type int [2] (which decays to int *) and &x has type int (*)[2].

In your second example, y is an int and p is an int *. These are separate variables, so each has a different address.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

First, for printing memory addresses, one should use %p instead of %d; using %d is actually undefined behaviour, even if it often works:

int main(){
    int x[2];
    x[0]=100;
    x[1]=200;
    printf("%p\n",(void*)x);
    printf("%p\n",(void*)&x);
    printf("%d\n",*x);
}

Second, &x does not introduce a new object / variable but uses the address of object x. The use of x decays to a pointer to the first element of array object x, and this is equivalent to &x and &x[0]. So same object -> same address, and therefore, x, &x, and &x[0] give you the same address.

In the other example, you introduce a new object p, which is different from object x. That's why &p and &x give different addresses

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
-1

What you heard is slightly incorrect.

An array is not "a constant pointer". An array is an array.

When you use an array in an expression, what you get is a pointer to the array's first element.

That pointer will have the same value (although a different type) as a pointer to the whole array.

These facts explain the results you saw.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • if the array name is not a constant pointer then why does `x +=3` is invalid? isn't it because this expression tries to modify the value of the array name using pointer arithmetic? I am quoting what is said [link](http://index-of.es/C++/C%20How%20to%20Program.pdf) page 276 – Dilini Peiris Mar 13 '18 at 17:58
  • 1
    @DiliniPeiris It may seem like a pedantic distinction, but it doesn't say an array name *is* a constant pointer; it just says it "can be thought of" as a constant pointer. It's an analogy that may make it easier to think about some things, but it breaks down if you think about it too much. For one thing, if an array name were a pointer, we would expect `x` and `&x` to have different values, which is what you were asking about in the first place. See also [question 6.9](http://c-faq.com/aryptr/constptr.html) in the [C FAQ list](http://c-faq.com/). – Steve Summit Mar 14 '18 at 11:19
  • @SteveSummit i am agree with you. you are absolutely right. only your ans in this question i found useful. Thank you bcz i think i learn something new from you –  Jul 14 '19 at 06:00
-1

You should use %p to print memory address

#include <stdio.h>

int main(){
   int y = 10;
   int *p = &y;

   printf("%p\n",&y);
   printf("%p\n",&p);
}

output :

0x7ffeed2dd6fc

0x7ffeed2dd6f0

by the way

int main(){
    int x[2];
    x[0]=100;
    x[1]=200;
    printf("%p\n",x); // u should use %p here
    printf("%p\n",&x); // this is the address of the array same as upper
    printf("%d\n",*x); // this is the value of x it's same as x[0]
} 
Damien Christophe
  • 115
  • 1
  • 1
  • 10