Accessing elements in Array of char pointers (char**)

I am trying to learn in more depth how an array of char pointers is accessed.
I have the following code that I am playing with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main()
{
    char *p[2] = {"cert", "turd"};
    *p = "sert";
    p[0][0] = 't';


    cout << p[0][0] << endl;

    return 0;

}


The line: *p = "sert"; works just fine and the string "cert" is modified to "sert" without error.

However the line: p[0][0] = 't'; throws a SIGSEGV segment fault and I dont understand why.

Why would changing the whole string work but changing 1 character causes a segment fault?

Is that something C++ does not support?
Can anyone tell me what is happening behind the scenes?

I have tried to look this up but I can not find a good explanation.

Thanks!
you have to do : strcpy(*p, "sert");
Thanks hannes!

But my problem is not with the line: *p = "sert";
This line works just fine.

I get the segement fault on this line: p[0][0] = 't';

Last edited on
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.
1
2
3
4
5
6
7
8
9
10
int main()
{
    const char *p[2] = {"cert", "turd"};
    cout << p[0] << endl;
    cout << p[1] << endl;


    return 0;

}
Last edited on
if you assign a 't' to an literal, then a segmentation fault raises. You have assigned "sert" to a pointer and that is a literal.
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.


Thanks for your help...
Last edited on
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.

http://cplusplus.com/doc/tutorial/dynamic/
http://cplusplus.com/doc/tutorial/arrays/
http://cplusplus.com/doc/tutorial/ntcs/
Thanks man!!!

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.

I appreacite you taking the time to help a noob..
Topic archived. No new replies allowed.