#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    11
    Rep Power
    0

    C++ string pointer problem


    Hi Everyone,

    I am trying to do something pretty basic:
    I've stored a string in a char* sptr pointer, and I have an array of char * pointers. I want to store the string in my sptr into the array, as follows:

    char * sptr = "Hello ";
    char * array [size];
    array[0] = sptr;
    sptr = "World";
    array[1] = sptr;
    cout<<array[0]<<array[1]<<endl;

    I want my output to be: Hello World
    but my output turns out to be: World World

    this is b/c I'm storing the sptr address not the string at this address, I've tried several things and nothings working. Can someone help me out please?

    thank you!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    C-style strings don't work that way. Your first two lines would work and do what you want. Your problem is being caused by the third and fourth lines.

    A C-style string is an array of characters terminated with a "null-terminator", a character whose ASCII code is zero (written '\0'). Since an array name is equilavent to a pointer, a character array can be referenced by a char pointer.

    However, a pointer must point to something. In particular, it must point to a block of contiguous memory locations that have been assigned to that pointer. In short, in order to initialize a pointer, you must allocate memory to it. In C++, you allocate memory with the new operator; in C, you did it with the malloc function. Basic rule: in C++, always allocate dynamic memory with new and always free it with delete.

    In your first line,
    char * sptr = "Hello ";
    you declare and initialize sptr. In this case, the compiler allocated seven bytes of memory for sptr to point to and it inserted into that memory block the string "Hello " with a '\0' as the seventh character.
    sptr now contains the location of the string "Hello ".

    Your second line is OK:
    char * array [size];

    Now, your third line,
    array[0] = sptr;
    is assigning the value of sptr to the first element of the array. array[0] now points to the same location as sptr, which is to the string "Hello ".

    Now it starts getting weird. In your fourth line,
    sptr = "World";
    you have the compiler create a constant string string as before, "World", and you assign it to sptr, which you then assign to array[1] in your fifth line.

    [NOTE: at this point, I realized that it should work and verified that fact with a test program.]

    Now, surprisingly that should actually work, though it's not the standard way for filling an array of strings. "Hello " and "World" should be in different locations, so assigning the second location to sptr should change its value and assigning sptr's value to the two array elements before and after that change should have them pointing to two different locations. Indeed, when I run it, it does actually work and gives the expected result of "Hello World".

    Yet from your output it appears that array[0] was somehow tied to sptr so that it changed when sptr changed, thus resultingin array[0] and array[1] pointing to the same string.

    Here's my test program. I took your code, provided a value for size, and echoed out the address locations of the strings and the locations that sptr and the array elements end up pointing to.
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        const int size = 4;
        char * sptr = "Hello ";
    
        printf("\"Hello \" is at %p\n",&("Hello "));
        printf("sptr == %p\n",sptr);
    
        char * array [size];
        array[0] = sptr;
    
        sptr = "World";
        printf("\"World\" is at %p\n",&("World"));
        printf("sptr == %p\n",sptr);
    
        array[1] = sptr;
        printf("array[0] == %p\n",array[0]);
        printf("array[1] == %p\n",array[1]);
        cout<<array[0]<<array[1]<<endl;
    }
    My output looks like this:
    "Hello " is at 00401230
    sptr == 00401230
    "World" is at 00401256
    sptr == 00401256
    array[0] == 00401230
    array[1] == 00401256
    Hello World

    Compile the program and see what your output looks like.

    Now, two ways that I think are better would be:
    Code:
        char * array [size];
        array[0] = "Hello ";
        array[1] = "World";
        cout<<array[0]<<array[1]<<endl;
    Or even better, since it is more general and gives the array its own copies of the strings:
    Code:
        char * sptr = "Hello ";
        char * array [size];
    
        array[0] = new char[strlen(sptr)+1];
        strcpy(array[0],sptr);
    
        sptr = "World";
        array[1] = new char[strlen(sptr)+1];
        strcpy(array[1],sptr);
    
        cout<<array[0]<<array[1]<<endl;
    
        // need to delete the new'd strings when you're done with them, usually in the destructor
        delete [] array[0];
        delete [] array[1];
    Last edited by dwise1_aol; June 24th, 2003 at 09:35 AM.

IMN logo majestic logo threadwatch logo seochat tools logo