PDA

View Full Version : not opengl related but I need help for my fps counter



Bra'Tac
05-11-2001, 12:38 AM
char *strcat( char *strDestination, const char *strSource );

Is there is similar fonction where strSource don't have to be CONST ???

ffish
05-11-2001, 01:31 AM
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.

ffish
05-11-2001, 01:39 AM
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.

Bra'Tac
05-11-2001, 03:06 AM
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).

Deiussum
05-11-2001, 05:30 AM
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?

Deiussum
05-11-2001, 05:40 AM
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");

Bra'Tac
05-13-2001, 10:51 PM
Am I stupid on this monday morning, but how can you concat two string if none of your example work?

Teofuzz
05-14-2001, 12:14 AM
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

ffish
05-14-2001, 12:58 AM
Most of us here use C++. I almost forget how to use C functions. Here's a demo:



#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>

using 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.

Deiussum
05-14-2001, 05:34 AM
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.

Bra'Tac
05-15-2001, 04:47 AM
Thanks you Deiussum. I was very stupid monday because it's dramaticaly logical !!