-1
 #include<stdio.h>
 main()
 {
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %u\n", arr, &arr[0], &arr);
    return 0;
 }

Its Output is:- 1200 1200 1200 I want to know how its answer is 1200?

I want to know how does it work??

  • 1
    Output could be literally anything, since you have Undefined Behaviour. Use `%p` for pointers in `printf`. – Paul R Oct 12 '16 at 08:14
  • 2
    Possible duplicate of [So you think you know pointers?](http://stackoverflow.com/questions/232303/so-you-think-you-know-pointers) – mch Oct 12 '16 at 08:18
  • @PaulR check the website http://www.studytonight.com/c/tests/3. Its showing answer 1200 1200 1200 that's why I wanna know how does it work? – Nishant Lamichhane Oct 12 '16 at 08:20
  • @Sillent_Killerzz: given that example I suggest you avoid that site in future. – Paul R Oct 12 '16 at 08:21
  • 2
    A lot of s... around the web... – LPs Oct 12 '16 at 08:23
  • 3
    @Sillent_Killerzz the first 5 questions on this site are garbage, the 6th question is the first which makes sense. The first is undefined behaviour, the answer of the second is "none of them", the answer of the third is "all miss a ; at the end", ... – mch Oct 12 '16 at 08:25
  • @mch Thank you for the information. I was also a little bit confused why its answer is that? Why are these sites giving wrong knowledge? – Nishant Lamichhane Oct 12 '16 at 08:27
  • @PaulR which is the best site to practice C programs? Can you help me? – Nishant Lamichhane Oct 12 '16 at 08:30
  • 2
    I wish we had something like a Stack Overflow blacklist over crappy sites. This studytonight.com site is awful! That test is written by a clueless beginner with completely outdated C knowledge. I peeked at their tutorial too and it is outdated by 25 years or so. Stay clear from that site! – Lundin Oct 12 '16 at 08:32
  • @Sillent_Killerzz: unfortunately there are a lot of bad sites out there - I suggest you get a good book instead - here is a [somewhat useful list](http://stackoverflow.com/q/562303/253056). – Paul R Oct 12 '16 at 08:32
  • @Lundin: yes, I just noticed a dubious book on there (Norton) - I don't know of a better list though ? – Paul R Oct 12 '16 at 08:34
  • @PaulR can you refer me a best website rather than a book? – Nishant Lamichhane Oct 12 '16 at 08:37
  • @Sillent_Killerzz: most tutorial web sites are garbage - you can try some of the beginner challenges at http://hackerank.com, or the [dailyprogrammer subreddit](https://www.reddit.com/r/dailyprogrammer), but if you're serious about learning to program then you'll need at least one good book. – Paul R Oct 12 '16 at 08:41

2 Answers2

1

arr &arr[0] and &arr all give the address of the array.

But the address of an array is not necessarily an integer, so your printf causes undefined behaviour.

The correct way would be: printf("%p\n",(void *)arr);. The actual output depends on your implementation and is most likely meaningless to you for any purpose for C programming.

Secto Kia
  • 990
  • 4
  • 12
0

arr gives the base address of array (starting memory address of arr)

&arr[0] gives the address of first element in arr which is 1200.

&arr is the address of the array itself (int arr[]={2, 3, 4, 1, 6};), so if you do &arr+1 it will points to memory just past the end of the entirety of your array (1200+sizeof(int)*5).

roottraveller
  • 7,942
  • 7
  • 60
  • 65