concatenating a char * without "

This is a discussion on concatenating a char * without "

Thread: concatenating a char * without "\0"

"
within the C++ Programming forums, part of the General Programming Boards category; haven't used C/C++ in a while....so this might be really simple for most of you.... can someone please tell me ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    7

    Question concatenating a char * without "\0"

    haven't used C/C++ in a while....so this might be really simple for most of you....

    can someone please tell me how to concatenate two char * strings?
    (one of them is const char* and the other one isn't)

    eg.
    char * c1 = "SYSTEM PATH = ";
    char *c2 = getenv("PATH");

    how do I concatenate c1 + c2 and put the result into a third char * variable?

    I've tried strcat() but since c2 points to a string without the explicit '\0' character, the program crashes.

    thanks very much.

    -sameer

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    int main()
    {
       std::string a = "SYSTEM PATH = ";
       char *b = getenv("PATH");
       if ( b )
       {
          a += b;
       }
       std::cout << a << std::endl;
       return 0;
    }
    Last edited by Dave_Sinkula; 05-09-2005 at 05:52 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Dave_Sinkula
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    int main()
    {
       std::string a = "SYSTEM PATH = ";
       char *b = getenv("PATH");
       if ( b )
       {
          a += b;
       }
       std::cout << a << std::endl;
       return 0;
    }
    ^those aren't two char* variables... but he does have a good point - since you're using C++, you can take advantage of the string class.

    AFAIK, there is no way to do that with char* variables unless you know the length of the first one...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Are you sure the return value of of getenv, which in this case is c2, isn't null terminated? I think it is. You need to allocate space for the combined string. Since neither of the buffers, c1 or c2, should be modified, you need to create a third buffer, allocate enough space for both strings and a null terminator, strcpy the first string into the result buffer, and then strcat the second string on to the end.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I agree with Daved. getenv returns a null-terminated string. If it didn't, the concatenation operator of the std::string class would not work correctly either. Consider this example:
    Code:
    int main()
    {
       std::string a = "SYSTEM PATH = ";
       char *b = getenv("PATH");
       if ( b )
       {
          a += b;
       }
       std::cout << a << std::endl;
    
       a = "SYSTEM PATH = ";   //reset a
       char c[10] = "123456789";   //create a string
       c[9] = '0';   //remove null termination
       a += c;   //concatenate
       std::cout << a << std::endl;
       return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What's worse, if it wasn't NUL-terminated, there would be absolutely no way to find out the length. There is no getenvlen function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM

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