+ 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 🙁?
22 odpowiedzi
+ 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 :)
+ 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)
+ 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.
+ 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
+ 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]={};
+ 8
@alice .. Tanay is wrong too.. only JPM7 is correct.
+ 8
Thank you Babak Sir, and nice to see you again dearest bro!
+ 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 : )
+ 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.
+ 6
Thanks to all who answered this question
+ 3
message received , thanks for your time.
+ 2
The output is 0@Timon Paßlick
+ 2
why is it zero? @ Timon Paßlick
+ 2
I'm still confused .
+ 2
Thanks @Shah Tanay , I got it 😄
+ 1
Thank you , I know the answer but I don't know how to get it
+ 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.
+ 1
Ok, got it. The comments were enough. Are these rules also valid for stl containers and initializer_lists?
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.
0
I knew all of that except that it depends on the containers constructor.