Copying data to char* out of function

This is a discussion on Copying data to char* out of function within the C++ Programming forums, part of the General Programming Boards category; Ok, the problem is I have a char* and in a function I set it to a variable. That variable ...

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    Copying data to char* out of function

    Ok, the problem is I have a char* and in a function I set it to a variable. That variable only lasts as long as the function does. The problem is it's a char* so it points to the address of the variable, right? So when the function ends, it's terminated and the pointer is pointing at nothing. So I need a way to copy this to the char*(string). strcpy just causes the program to crash because it doesn't seem to like char*s with no length. How could I do this?
    Using Dev-C++ on Windows

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    As long as it is a null terminated ('\0') string, strcpy should be fine. Post some code so we can better diagnose the problem.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    This is another reason using the C++ string everywhere is easier.

  4. #4
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Code:
    char temp[arg.length()];
    strcpy(temp, arg.c_str());
    strcpy(settings->profile.name, temp);
    If I try to copy temp into settings->profile.name, Windows gives me the old, this program has caused an error and must close. If I use settings->profile.name = temp, I assume it works, but gets cut when the function ends because it ends up being set to "".
    name is in a struct called Profile, which is in a struct Settings. name looks like so:
    char* name;
    It's never set to a certain sized array so that's probably the problem with strcpy, but it works when I assign it to a string in quotes or another char* variable.
    Using Dev-C++ on Windows

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It's never set to a certain sized array so that's probably the problem with strcpy, but it works when I assign it to a string in quotes or another char* variable.
    You hit the nail on the head, so to speak, as far as the problem. No memory is ever allocated for name. (name = temp) tells the pointers to point to the same place, but the data in that location gets reclaimed when temp goes out of scope, so you have no control over what ends up there. If it continues to work as it should, then that is just a fluke. You'd have to allocate memory with new[], and deallocate with delete[].

    My advice:
    Quote Originally Posted by Daved
    This is another reason using the C++ string everywhere is easier.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >char temp[arg.length()];

    arg.length() needs to be known at compile time. I doubt that it will be though. You will probably need to use dynamic memory allocation if you want to size temp "just right", or declare temp of a size that is very unlikely to be too small at compile time, ....or try to use C++ strings instead of C style strings if at all possible.
    You're only born perfect.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    When dynamically creating space for a C style string based on the length of another string (C or C++ style), you must add 1 for the terminating null. If you choose to continue using C style strings and end up dynamically allocating them, make sure to add one to the length to keep room for the null.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21