C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 24th, 2003, 07:31 AM
lenochka lenochka is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 11 lenochka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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!

Reply With Quote
  #2  
Old June 24th, 2003, 09:30 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,836 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 6 h 59 m 13 sec
Reputation Power: 446
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C++ string pointer problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway