Well I have seen declarations like
char strng[20]="";
strcpy(strng,"Some_String");
Here it is well understood that when I declare strng[20]
, i am reserving 20 memory locations(bytes) for this character array so I can copy any string to it which is almost 20 characters long.
Also, a declaration like
char *strng;
strng=(char*)malloc(sizeof(char)*n);
Which will create a character pointer(2 bytes), and then memory is allocated viamalloc
and the base address of the allocated memory is assigned to strng
. Here,n
bytes will be allocated.
But, I have seen declarations like
char *srtng;
strng="Some_Very_Long_Text";
So, How is memory being allocated in this case? Here I am declaring a char pointer(2 bytes) then assigning a string to it. If the size of this string isn
, then how and where is this string stored? my pointer only holds its base address. when is memory allocated and how? I am not even using malloc
here.