Don't understand exactly how char * months[12] works

I think I have a pretty good grasp on pointers and arrays and all that stuff, but this statement is confusing me:
1
2
char * months[12] = 
	{"January" , "February", "March", "April", "May", ..., "December"};


I don't understand how this works. It creates 12 point-to-char pointers, which are used like normal character arrays. I understand that part of it, what has me confused is how does the computer know how much room to give.
For example, if you have something like this:char arrayName[20] = "some cool example";
Then the computer knows it needs to save space for 20 type char variables, in which the string goes. But with the previous statement, how does the computer know how long each string is? It knows that there will be 12 strings in the array, but I don't see how it knows the length of each string and since this isn't using the string class, it kind of needs to know that to prevent overflow.

The code you posted actually shouldn't work, because string literals are const. Some compilers will let you get away with that because older code uses it, but you shouldn't do that in any code that you write.

The proper way would be to do this:

1
2
const char * months[12] =   // note the const
	{"January" , "February", "March", "April", "May", ..., "December"};



As for your actual question, the string data doesn't go in a variable like it does when you say char foo[20] = "whatever"; Instead, string literals used by the program get stored somewhere separately in the program's executable. Like if you opened up your .exe file in a hex editor and looked through it, you would find "January", "February", etc written in it somewhere, along with all the other string literals your program uses

Therefore the computer doesn't need to reserve any space for the characters because the characters already exist in the exe. You just point to those already-existing characters.
Oh, I see. I would have never thought of it that way, but it actually makes sense. Thanks for the help.
Topic archived. No new replies allowed.