Thread: Function with char *

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    54

    Function with char *

    if i have a class that has this function

    Code:
            void SetText(char *txt){Text=txt;};
    and this memeber

    Code:
    	char *  Text;
    and i then want to call the function

    why do i have problems if i do something like this:

    Code:
    char temp[256];
    int num = 100;
    sprintf(temp,"number is: %d",num);
    object->SetText(temp);

    when i tried to do this the objects member Text just go set to nothing.

    is there something wrong with my code?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you have a class, then it's C++, not C, and you probably should be using std::string and not char *'s whenever possible.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    is there a std::string function like sprintf ??

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me guess, the code with sprintf() is in a function that gets called from somewhere else, and when you look at "Text", it's outside this function - am I correct?

    If so, you are storing the address of "temp", which is a local variable in a function. This variable doesn't exist once you are outside the function, and the memory that you've save will be overwritten by other local variables in some other function at the next function call.

    You probably want allocate some memory and copying the string instead...

    --
    Mats

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by e66n06 View Post
    is there a std::string function like sprintf ??
    You would use stringstreams I think instead of a direct function call.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    Text is the memeber of the class

    also i thought string streams are for cout and cin ??

    how can i easily input an int into a string? to do i have to use itoa()

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    the thing i found was it worked if i did:

    Code:
    object->SetText("hello");
    but not

    Code:
    object->SetText(temp);

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Because similar to what matsp said, you're messing around with local variables that get corrupted. String literals are generally read-only memory stored somewhere special.

    Using std:string would make this slightly easier for you to handle without knowing the underlying mechanisms (although you can still break it).

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, because temp is a local variable in a function, and thus "disappears" after you leave that function.

    As to how to use stringstream:

    Code:
    ....
      stringstream ss;
      string temp;
      int num = 100;
      ss << num;
      ss >> temp;
    ...
    Note that temp is still a local variable, so you still need to copy (the content of) it to make it "permanent".

    --
    Mats

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    so temp is a variable local to the function, so it will dissapear right?

    but the call to

    Code:
    object->SetText(temp);
    happens before the function finishs

    so i don't understand why it does not work?

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Let's work with something easier as an example. Say an int. Let's say we have this code:

    Code:
    int x = 5;
    dosomething(&x);
    Now let's say x has an address of 0x12341234. Fair enough? Now the function dosomething() is defined like this:

    Code:
    void dosomething(int *p)
    {
    	someglobalintptr = p;
    }
    That saves the value of the address passed to it, right? That means someglobalintptr will contain the value 0x12341234.

    Let's say later on in your code, a function is called that uses local variables. Let's pretend it's called performcalculations() and is defined something like this:

    Code:
    void performcalculations()
    {
    	int y = 100;
    
    	.....
    }
    y might now have the address 0x12341234. Because x, the local variable of the first section of code is gone, the memory will be reused by other local variables (I'm making this extremely simple).

    So later on if you print the value of someglobalintptr:

    Code:
    printf("*someglobalintptr = &#37;d\n", *someglobalintptr);
    You'll end up what is inside address 0x12341234. Which could be y instead of x.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by e66n06 View Post
    so temp is a variable local to the function, so it will dissapear right?

    but the call to

    Code:
    object->SetText(temp);
    happens before the function finishs

    so i don't understand why it does not work?
    But you are taking the address of temp (because temp is an array, it is passed as the address of the first element), and you are stuffing that address into Text. Text now contains the address of temp. Fine. Now we leave this function, and temp is no longer valid - the memory address is obviously still ok to be used, but at some point, it will be re-used for something else - so the content of the memory that was temp a little while ago, is now overwritten with something else - highly probably not even a text-string, and thus your "Text" is no longer pointing to a text-string.

    --
    Mats

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    ok, i think i understand now,

    it's because the function (char* txt) takes an address, not an actuall string of text.

    my only question is, so why does it work for something like this

    object->SetText("hello");

    also thanks very much for the help

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    As I said already, string literals, which are the text with quotes around them, are really stored somewhere else. They are not stored with local variables.

    For example:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char *szMsg = "Testing, 1 2 3...\n";
    	printf("szMsg = &#37;s\n", szMsg);
    	return 0;
    }
    Now what really happens is the compiler will take "Testing, 1 2 3...\n" and put it in a special location which is generally read-only. It will take the address of it, though, and put the address inside szMsg. Since the string literal is always available because it's part of the .exe or equivalent binary executable, you're ok.

    The variable szMsg may disappear when its function ends, but the contents, which is an address of a location of the string literal "Testing, 1 2 3...\n" is still valid after a local function ends (although that's kind of pointless for an example with main()).
    Last edited by MacGyver; 08-16-2007 at 02:54 PM.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because "hello" is a "string literal", which means that it's a piece of text that you put in the source-code, and it's stored somewhere in the binary (executable) file that gets loaded when you run your program. The address of this is a permanent place in the memory in your applicaton, so it doesn't get reused for something else when you leave the function.

    Actually, if you were to do something like this:
    Code:
    object->SetText("Hello"); 
    object1->SetText("Hello");
    ...
    object9->SetText("Hello");
    You would probably find that all of the "Text" members of the ten different objects are all pointing to the same place (slightly dependant on the compiler).

    You would also find that if you tried to do something like this:
    Code:
    ...
       Text[1] = 'a';
    ....
    This would fail, rather than change the text from "Hello" to "Hallo" as you'd expect it to do.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM