Thread: string = char pointer ( is it safe??? )

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    string = char pointer ( is it safe??? )

    Code:
        char* stupid = getenv("HOME");
        string filepath = stupid;
        cout << "Your home directory are " << filepath << endl;
    Is is safe to do so?
    I have to use char pointer because compiler complain if I use char array because compiler cannot know the size of char array. I mean, I have to be very careful if I have to deal with pointer.
    so
    string = char pointer;
    Is it safe??????
    Or is there a better way inisialize string c++-style from string c-style?
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> safe to do so?
    No, but only because getenv() might return NULL.

    You could do something like this instead:
    Code:
        char* stupid = getenv("HOME");
        string filepath = (stupid == NULL) ? "[not set]" : stupid;
        cout << "Your home directory is " << filepath << endl;
    gg

  3. #3
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Code:
    cout << "Your home directory is " << getenv("HOME") << endl;

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Wrong.
    It is illegal to insert a NULL C-string pointer into an ouput stream.

    gg

  5. #5
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Oh, okay. I've never tried that, I just assumed.

  6. #6
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Upon saying that, I decided to try:
    Code:
          1 #include <iostream>
          2 #include <unistd.h>
          3 
          4 int main()
          5 {
          6   std::cout<<getenv("HOM")<<std::endl;
          7   return 0;
          8 }
    It didn't core dump on me ("HOM" is a nonexistant variable), nor did I get compile time errors/warnings.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Had to look it up myself, 27.6.2.5.4 - 3

    gg

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You're putting too much faith in your std::lib implimentation.....just go to the source if you need to verify something

    gg

  9. #9
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Thanks for that link. =)

  10. #10
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Quote Originally Posted by Russell
    Code:
    cout << "Your home directory is " << getenv("HOME") << endl;
    Well, actually this example is to simplify my problem. The problem is.....

    I have this function:

    void myFunctionThatOnlyAcceptStringArgument( string HomeDirectory );

    And the string HomeDirectory is taken from this function:
    getenv("HOME");

    You know, I make a C++ program that have to deal with system. So I have to mix C code ( to deal with system, you know system programming use char* string not C++ style string ) and C++ code ( the program it self ).

    Thank you............ and thank for Codeplug
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM