“Consider All declared in main().”
Then
1) Yes.
2) Yes.
3) Yes, and no (it’s stored neither on the stack nor in the heap in common implementations). “i.e. allocates 6 bytes” — you seem to have forgotten about the memory required for the pointer. Also, there’s an erroneous claim in the comments and in another answer that char* str= "Hello";
is wrong, but in fact it is legal C and, for now, legal C++ … see What is the type of string literals in C and C++?
4) True, but it would be false if you changed 10 to 5 — that is, given char str[5]="Hello";
, str
is not NUL-terminated.
5) False and no (although the implementation might store a NUL following the string — C++11 requires it — but that isn’t part of the string).
“I am asking this question with respect to where they are stored heap or stack?”
Where do people get the idea that these are the only sorts of memory? Local variables are stored on the stack and memory allocated via malloc
or (non-placement) new
is allocated from the heap. Program code, file-scope variables, and literals fall into neither of those categories.
solved C Style Strings Difference : C/C++ [duplicate]