I'm confused as to how memory allocation works in different scenarios in C.
For example, let's say I've created:
char string[7];
char *store[8];
and then, I have "string" be "123456\0".
When I have store[0] = string;
, store[0] successfully stores the address of the 1st character of "string", which when printed using %s correctly prints "123456".
But, when I do strcpy(store[0], string)
, I get a segmentation fault error because I'm not accessing memory that I'm allowed to touch.
However, when using the 1st method (store[0] = string
), am I not already allocating and reserving memory to create store[0]? What's the difference in memory handling here?
Does store[0] not already exist in either situation, since I've created store[8] via char *store[8]
in the beginning, which should produce an array of 8 strings full of (garbage) data?
Apologies if my question is unclear. Any help is greatly appreciated.