Line 6 is undefined behavior so it is a problem. You are creating a 1 dimensional array of char pointers and each points to a constant string literal. You cannot dereference that and try to overwrite the strings.
Secondly you have not created a 2 dimensional array so line 7 and 10 is also undefined I think. Again on line 7, even if it was a 2 d array you are still trying to overwrite a const string literal which is undefined behavior.
Actually, what hannes has suggested is also undefined behavior. You only have memory allocated for 2 character pointers. There is no memory allocated for copying a string. You would have to call operator new twice to initialize each array first.
This should work for read access. You can't change either of the strings pointed to in this case since they are const literal strings.
Hey guys,
Ok I got why I cant change the strings pointed to.
And based on the comments from kempofighter I realized part of my trouble is my understanding of the relationship of pointers and arrays.
Do either of you know of a good place to get info on how arrays are actually stored in memory?
Kempofighter, you said that char *p[2] = {"cert", "turd"}; is not a 2 dimensinal array...
But doesnt it work out to look like a 2D array in memory? If its not a 2D array why does **p produce the first letter of the first array and the same for p[0][0]
isnt a 2D array just a 1D array of pointers pointing to a row of "T" (type).
*** EDIT*** Forget the above line I have corrected myself on this with some testing.
Are you aware of the tutorials section on this site? You are diving into some hairy subject matter, which is commendable. Good for you! 2d arrays are trickier when dealing with characters due to the special nature of C style strings. Yes it is possible to do 2d arrays with pointers and operator new but I can't find the article that explains that right now. When the article database is back up, browse through that and you'll find a good one on 2d arrays.
I will check the articles... I have been playing with pointers and arrays quite a bit today and your right about the nature of the C-strings... It is all making more sense now. It helps to see how the arrays are stored in memory at least on my platform. I would still like to find good article on array equations and array decay... the ones I have found lack clear grammer and examples.