You are not logged in. Please login at to post your questions!


Pointers and Array in C.

Consider this code fragment :

int main()
   int a[4]={1,2,3,4}
   printf("%d %d %d",a,&a,*a);

This code is giving unexpected result!

The value of "*a" is 1, which seems to be correct.

But how come compiler is giving similar values of "a" and "&a"??

And even if "a" and "&a" have similar values, then in that case, should't "*a" be the same as "a" and "&a" ?? Since "a" is pointing to itself.

Please Explain. Thank You.

asked 18 Mar '15, 17:45

coder3373's gravatar image

accept rate: 0%

Arrays are basically pointers. The name of the array is the first memory location from where the array begins.

In printf("%d %d %d",a,&a,*a);

a represents the address of the location from which the array starts. &a is the usual way of saying the address of a, which is again the start location of the array. This is why they are giving similar answer. The last one, *a is the value at pointer a, i.e. the value at the location from which the array starts, which is the first element of the array.


answered 18 Mar '15, 19:35

dragonemperor's gravatar image

accept rate: 10%

Your answer seems good. But in this case &a means "address of a". a is itself an address. So it just implies "address of an address" which is quite absurd. Isn't it?

(18 Mar '15, 20:06) coder33730★

In printf , since you have used %d , a and &a would give garbage values. Check here. In your case, they are just giving same garbage values as they are pointing to the same address location. To know more about pointers in C/C++ , read this and this.


answered 18 Mar '15, 19:16

shivam217's gravatar image

accept rate: 20%

edited 18 Mar '15, 20:12

Thanks for the answer. But I feel that's not the reason. %d just gives decimal system equivalents of the address, not garbage values.

(18 Mar '15, 19:29) coder33730★

I mean,since the decimal representation of starting address of a is out of int range , it is giving garbage value. And if we use %lld in printf , we will get correct decimal representation of address. See here.

(18 Mar '15, 20:08) shivam2174★

ok now i got your point. but %d works with my compiler as the address does't exceed the int range.

(19 Mar '15, 01:01) coder33730★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 18 Mar '15, 17:45

question was seen: 1,220 times

last updated: 19 Mar '15, 01:01