+ 13

C++ code question ( 8 )

What is the output of this code? int a[5] ={1,2,3,4}; int *p = a; cout<<*(p + 4); This question maybe is too simple for you, but it's a stumbling block for me , could you give me a hand 🙁?

8th Mar 2018, 8:04 PM
Eros boulay
Eros boulay - avatar
23 Answers
+ 13
well there you go, :) one would have assumed uninitialised array values would produce garbage in the same way uninitialised variables do :) but, no. clearly they are auto assigned 0 The more you know :)
8th Mar 2018, 9:13 PM
jay
jay - avatar
+ 11
Arrays are contiguous chunks of memory filled with homogeneous types ( in your sample with 32Bit integer types ). int a[5] ____ |__1_| <- a[0], address -> 0x0021FCC4 |__2_| <- a[1], address -> 0x0021FCC8 |__3_| <- a[2], address -> 0x0021FCCC |__4_] <- a[3], address -> 0x0021FCD0 >>>>>>> last element is filled with zero by default. |__0_] <- a[4], address -> 0x0021FCD4 int *p = a or 0x0021FCC4; print -> content of the address of the last element which pointed by p (if *p dereference 1, then *(p+1) dereference 2, *(p+2) dereference 3 all the way down to the last element)
9th Mar 2018, 5:14 AM
Babak
Babak - avatar
+ 10
you have declared array of 5 element but you only assigned value to the first 4 elements so....by default compiler initialize 5th element with 0. but if you will try to print values outside the array range like*(p+5) or anything then it would print garbage values.
8th Mar 2018, 8:54 PM
Tanay
Tanay - avatar
+ 10
int most languages if some array values are not defined they automatically set to default value of that type of array(array values), so in this case output is 0
8th Mar 2018, 11:24 PM
Vukan
Vukan - avatar
+ 9
Hey people, don't confuse undefined behaviors whit yours gaps! Please do some checks before talking about it. You won't learn anything if you do so. Very good @JPM7 you know your stuffs! I was just reading it, in sum if you want to initialize an array at zero you could do like this: int arr [n]={};
8th Mar 2018, 10:31 PM
AZTECCO
AZTECCO - avatar
+ 8
@alice .. Tanay is wrong too.. only JPM7 is correct.
8th Mar 2018, 10:37 PM
AZTECCO
AZTECCO - avatar
+ 8
Thank you Babak Sir, and nice to see you again dearest bro!
9th Mar 2018, 7:02 AM
AZTECCO
AZTECCO - avatar
+ 8
Late to the Discussion, not here for adding anything, instead, learn from everyone here, just wanted to thank @All, I learned something new (again) from this discussion. kinda wish we could have loads more of such discussions like this, something worth reading and knowing. I'm definitely bookmarking this thread. Thanks @Everyone : )
10th Mar 2018, 7:22 PM
Ipang
+ 7
thank you for knowing what you are talkin about lol Whit respect for people that didnt, I understand.. we all want to help but sometimes we do damages.
8th Mar 2018, 11:22 PM
AZTECCO
AZTECCO - avatar
+ 6
Thanks to all who answered this question
8th Mar 2018, 9:27 PM
Eros boulay
Eros boulay - avatar
+ 3
message received , thanks for your time.
9th Mar 2018, 6:02 AM
Eros boulay
Eros boulay - avatar
+ 2
The output is 0@Timon Paßlick
8th Mar 2018, 8:33 PM
Eros boulay
Eros boulay - avatar
+ 2
why is it zero? @ Timon Paßlick
8th Mar 2018, 8:36 PM
Eros boulay
Eros boulay - avatar
+ 2
I'm still confused .
8th Mar 2018, 8:37 PM
Eros boulay
Eros boulay - avatar
+ 2
Thanks @Shah Tanay , I got it 😄
8th Mar 2018, 9:23 PM
Eros boulay
Eros boulay - avatar
+ 1
Thank you , I know the answer but I don't know how to get it
8th Mar 2018, 8:30 PM
Eros boulay
Eros boulay - avatar
+ 1
@JPM7 When I say that something is wrong and misleading, I use to give very good reasons. Is it because I said that it is out of the array although technically speaking, it's inside the array but uninitialized? Come on, she's a beginner! She wanted a simple explanation, nothing is entirely true.
8th Mar 2018, 8:58 PM
Timon Paßlick
+ 1
Ok, got it. The comments were enough. Are these rules also valid for stl containers and initializer_lists?
9th Mar 2018, 12:19 PM
Timon Paßlick
0
They do, but it's just 0 in many cases because memory looks like this (power off) before you assign the first variable to it. It's undefined because for example deleted values don't get set to 0.
8th Mar 2018, 9:22 PM
Timon Paßlick
0
I knew all of that except that it depends on the containers constructor.
9th Mar 2018, 3:38 PM
Timon Paßlick