Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: not opengl related but I need help for my fps counter

  1. #1

    not opengl related but I need help for my fps counter

    char *strcat( char *strDestination, const char *strSource );

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

  2. #2
    Senior Member Regular Contributor
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    431

    Re: not opengl related but I need help for my fps counter

    If you're using C++ you can const_cast it:
    Code :
    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.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    431

    Re: not opengl related but I need help for my fps counter

    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.

  4. #4

    Re: not opengl related but I need help for my fps counter

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

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: not opengl related but I need help for my fps counter

    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
    Software Engineer and OpenGL enthusiast

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: not opengl related but I need help for my fps counter

    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");
    Deiussum
    Software Engineer and OpenGL enthusiast

  7. #7

    Re: not opengl related but I need help for my fps counter

    Am I stupid on this monday morning, but how can you concat two string if none of your example work?

  8. #8
    Senior Member Regular Contributor
    Join Date
    Sep 2000
    Location
    Italy
    Posts
    124

    Re: not opengl related but I need help for my fps counter

    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

  9. #9
    Senior Member Regular Contributor
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    431

    Re: not opengl related but I need help for my fps counter

    Most of us here use C++. I almost forget how to use C functions. Here's a demo:
    Code :
    #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.

  10. #10
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: not opengl related but I need help for my fps counter

    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.
    Deiussum
    Software Engineer and OpenGL enthusiast

Page 1 of 2 12 LastLast

Similar Threads

  1. related to OpenGL SC 1.0.1
    By Chaitra CJ in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-07-2012, 04:57 AM
  2. Not directly OpenGL related but...
    By MPech in forum OpenGL: Linux
    Replies: 1
    Last Post: 04-12-2002, 01:55 AM
  3. Not opengl related but can somebody help???
    By mohsin in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-21-2001, 12:48 PM
  4. MFC problem (not really OpenGl related... please help)
    By clucches in forum OpenGL: Advanced Coding
    Replies: 16
    Last Post: 04-20-2001, 09:18 AM
  5. New OpenGL related site!!!
    By romka in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 02-25-2000, 02:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean