What is the difference between C Style Strings
char str[10]="Hello";
char str[]="Hello";
char* str= "Hello";
1) I believe that char str[10]="Hello"
is automatic variable and stored on the stack.True? i.e. Allocates 10 bytes
on stack.
2) Does char str[]="Hello";
is also stored on stack? i.e. allocates 6 bytes - including null character
on stack.
3) Does char* str= "Hello";
stores pointer str on stack and the object "Hello"
is stored on heap? i.e. allocates 6 bytes - including null character
on heap.
4) All strings (in question 1,2 and 3)
are null terminated . True/False?
5) Whether it is C or C++
whenever we create an string like "Hello"
, it is always null terminated. Suppose in C++
we declare string str = "Hello";
, is it also null Terminated?
EDIT
Consider All declared in main().
@Negative points and close requests. I am asking this question with respect to where they are stored heap or stack? And also null termination.