"
This is a discussion on concatenating a char * without " " 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 ...
-
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
-
Just Lurking
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 04: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.*
-
Registered User

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...
-
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.
-
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
-
Cat without Hat
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
Similar Threads
-
By caldeira in forum C Programming
Replies: 31
Last Post: 09-05-2008, 01:01 AM
-
By ltanusaputra in forum Windows Programming
Replies: 3
Last Post: 03-01-2008, 01:06 PM
-
By compudude86 in forum C Programming
Replies: 6
Last Post: 07-23-2006, 08:54 PM
-
By willc0de4food in forum C Programming
Replies: 4
Last Post: 09-14-2005, 05:59 AM
-
By OmniMirror in forum C Programming
Replies: 4
Last Post: 05-14-2003, 09:40 PM