char *strcat( char *strDestination, const char *strSource );
Is there is similar fonction where strSource don’t have to be CONST ???
char *strcat( char *strDestination, const char *strSource );
Is there is similar fonction where strSource don’t have to be CONST ???
If you’re using C++ you can const_cast it:
char *some_string = strcat(strDestination, const_cast<const char *>(strSource) );
That should work. Alternatively, make a const copy of strSource and pass that to strcat instead.
Hope that helps.
Err … I should have read my texts first. const_cast only removes a const qualifier. You could try the static_cast<const char *>(strSource) cast, or make a temporary const copy of the source like I mentioned above. But I’m surprised that the compiler is complaining about your strcat call anyway. In my experience, if you have a const char * and you pass it to a function which requires a char * it will complain, but the other way around it shouldn’t. What I would expect is that the function implicitly promotes a char *strSource to const char *. It shouldn’t matter since the function isn’t trying to modify strSource. You could always go back to the C brute force (const char *)strSource convention.
the compiler doesn’t complain but an error occurs during execution. And I use C and not C++ (ok I know that I have to come to C++, but I don’t have time for the moment).
I don’t see how your runtime error could be caused by passing a non-const char* to a function that takes a const char*. What makes you think this is the problem?
One additional note, you COULD end up getting a run-time error if you are doing either of the following.
// This will cause an error because string literals
// are stored in a special memory space that should
// be treated as const and not changed.
char* blah = “Test”;
strcat(blah, “-More”);
// This will cause an error because you do not
// have enough memory allocated on the stack
// for the concatenation to occur.
char blah[5] = “Test”;
strcat(blah, “-More”);
Am I stupid on this monday morning, but how can you concat two string if none of your example work?
I use to write my own functions… (in C++)
Allocate some memory (operator ‘new’ in C++, fantastic!) when you need to concatenate string literals, and copy.
tFz
Most of us here use C++. I almost forget how to use C functions. Here’s a demo:
#include
#include
#include
#includeusing namespace std;
// Uses C strings.
void testchar()
{
char *string1 = (char *)malloc(sizeof(char)*12);
char *string2 = “string”;
strcpy(string1, "test ");
strcat(string1, string2);
cout << "Char * concatenation: " << string1 << endl;
free((void *)string1);
}// Uses C++ strings.
void teststring()
{
string string1("test ");
string string2(“string”);
string string3 = string1 + string2;
cout << "String concatenation: " << string3 << endl;
}int main(int argc, char **argv)
{
testchar();
teststring();
return 0;
}
Doesn’t have much to do with OpenGL, though.
Originally posted by Bra’Tac:
Am I stupid on this monday morning, but how can you concat two string if none of your example work?
I was trying to show you examples of what you might be doing to cause the error. I think I’ve said this on these boards a few times now, but I’ll say it again. They key to mastering C/C++ is understanding how to utilize memory. You always have to be aware of where your memory is coming from. In the two examples I gave, you do not have sufficient memory allocated for the concatenation, and/or the memory is in a protected space. Here is an example that WILL work for you.
char blah[256] = “test”;
strcat(blah,“-More”);
blah has 256 bytes allocated on the stack. The length of “test-More” is far less than this so you will have no problem with using that strcat.
Thanks you Deiussum. I was very stupid monday because it’s dramaticaly logical !!