char* to string object

This is a discussion on char* to string object within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to create a string object from a char array. My char array has "This house is ...

  1. #1
    Unregistered
    Guest

    char* to string object

    Hi,

    I am trying to create a string object from a char array.

    My char array has "This house is nice"

    string tempStr(MyCharArray);

    tempStr now has "This", which is wrong!


    How can I can put the whole line in my string object? thanks.

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    I don't know why that is happening but char* isn't an array. Thats a pointer. char blabla[50] is an array

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    The following code works fine for me. I'm using MSVC++ 6.0, what about you?

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char* MyStringArray = "This house is nice.";
        string tempStr(MyStringArray);
        cout << endl << tempStr << endl;  // Prints out "This house is nice."
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Unregistered
    Guest
    Originally posted by ErionD
    I don't know why that is happening but char* isn't an array. Thats a pointer. char blabla[50] is an array
    I am aware of the difference. Here's the code snippet:

    <code>
    nom = new char[50];
    char* ptr = &tamp[4]; // location 4 is where the name begins

    while (ptr)
    {
    if (isspace(*ptr) || *ptr == '\0')
    nom[i++] = ' ';
    else
    nom[i++] = *(ptr++);

    if (isspace(*ptr) && isspace(*(ptr + 1)))
    {
    nom[i] = '\0';
    string nomm(nom);
    prod->setNom(nomm);
    break;
    }
    }
    </code>

    basically, I am reading a name of a product from a file. The name is multi names separated by space and ended by double spaces.

  5. #5
    Unregistered
    Guest
    do you guys know of a way to tokenize on double space?

    The code I posted above is ugly and doesn't really do the job. If I use strtok(myStringArray, " "), it will still tokenize on ONE space rather than two spaces.

    Thanks for the feedback y'all.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think your code is flawed, because as soon as it hits a space, ptr is no longer incremented:
    Code:
    if (isspace(*ptr) || *ptr == '\0') 
       nom[i++] = ' '; 
    else 
       nom[i++] = *(ptr++);

  7. #7
    biz
    biz is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    14
    Originally posted by ErionD
    I don't know why that is happening but char* isn't an array. Thats a pointer. char blabla[50] is an array
    This is actually a tad incorrect. array[50] actually defines a pointer which points to the first item in an array. Example:
    char blah[10]="hello";
    cout << blah << endl << (blah+1) << endl;

    This would produce:
    hello
    ello

    That is why you specify char* when passing an array of character to a function/method, because in actuality it is a pointer to the start of the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21