Thread: possible to return a string ?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Location
    Quebec city
    Posts
    5

    possible to return a string ?

    I'm not used at all to the fact that strings aren't supported in C like they are in other languages. I have a 2 dimensionnal array with words and I want to return one of the array's word and it doesn't work. I have compiler errors like "return makes integer from pointer without a cast" and "function returns address of local variable" so my guess is that it's returning the addess of the local variable :P

    What confuses me is that I can print the word but not return it. I know this could be done with a pointer but I'm wondering if it's possible to do it without.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char getWord() {
    	char a_words[5][20];
    	
    	strcpy (a_words[0],"nucleaire");
    	strcpy (a_words[1],"patate");
    	strcpy (a_words[2],"verre d'eau");
    	strcpy (a_words[3],"table");
    	strcpy (a_words[4],"compagnon");
    
    	printf("%s", a_words[2]);
    	
    	return a_words[2];
    }
    
    int main() {
    	char word[20];
    	strcpy(word ,getWord());
    	printf("%s", word);
            return 0;
    }
    thanks
    Last edited by _nav; 08-19-2005 at 12:58 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Variables declared in functions are local to that function. The space for them is allocated on the stack and when the function returns that stack space is freed automagically which means that anything that was stored there is no longer valid. That's what the compiler is warning you about.

    There's a few ways to get around this:
    1) Pass a buffer to the function for it to store the word in.
    2) Use malloc() to allocate the space for the words. This uses memory in the heap instead of the stack. Just be sure to free() the memory when you're done with it.
    3) Declare your variable as static (e.g. static char a_words[5][20];). Variables declared as static live for as long as the program is running.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Quebec city
    Posts
    5
    2) Use malloc() to allocate the space for the words. This uses memory in the heap instead of the stack. Just be sure to free() the memory when you're done with it.
    Wouldn't that result in using a pointer ?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Yes. The return from malloc is a pointer to the allocated memory (if the space is successfully allocated).

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Quebec city
    Posts
    5
    I'll do that then if it isn't possible to do it without
    thanks !

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Just declare yoru function as
    Code:
    char* getWord() {
    Didn't kermit just post that? Hm. This way, it returns a char pointer to the strings that you made in your function and it'll work. Also, in regards to the above, the way he is doing it is "pretty" safe. He used strcpy to copy the buffer returned from the function to a local one in main, which is fine. If he had directly been accessing the char* returned from his function, then it would mostly likely be overwritten with stupid stuff from another function call, but he didn't.

    @new posts of _nav: C strings are all pointers. A C string is character pointer to some data, a string.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Quebec city
    Posts
    5
    I didn't knew C strings were all pointers, I'm really not at ease with that yet, all programming languages I'm familiar with support strings so I have to work on that.

    If you're reffering to my code, I'm a she btw !

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I'll refer to everyone on the internet as "it" now. Well, yeah, the concept of a pointer, and how data is stored in memory, might make sense over more time, but for now, this article about regular C strings (null terminted character arrays) gives you are very good idea of how things work: http://www.cplusplus.com/doc/tutorial/tut3-2.html speaking of howstuffworks: howcstringswork

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Quebec city
    Posts
    5
    thanks for the links I'll read that.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Tonto
    Also, in regards to the above, the way he is doing it is "pretty" safe. He used strcpy to copy the buffer returned from the function to a local one in main, which is fine. If he had directly been accessing the char* returned from his function, then it would mostly likely be overwritten with stupid stuff from another function call, but he didn't.
    It's not safe in any way. It's undefined and that's never safe. Don't you think strcpy() uses the stack? strcpy() expects two pointers to be passed to it (dest and src) and those get stored on the stack (not to mention any other local variables strcpy() uses.) Blamo, there goes whatever was in that part of the stack in the getWord() function.

    Please don't make it sound like it's sometimes okay to use already-freed memory because it's not. Ever. The C standard says its use is undefined which means that you may or may not be able to get away from it but you should never ever depend on it being okay. A conforming compiler could even zero-out stack space as it's freeing up that memory if it wanted to and still be compliant with the standard.

    Never ever use memory contents that may or may not exist anymore.
    Last edited by itsme86; 08-19-2005 at 01:53 PM.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> Don't you think strcpy() uses the stack?

    Of course I do, and if she were to have returned array[5] than you would get some sort of error, because one of the pointer arg's pushed from the strcpy call would overwrite the important data. I know that this stuff "pmg out of the question noob rtfm!" stuff, however, this is one of those cases where it "just works", because of the stupid low level stuff going on. Despite the utter fact that it's blasphemy. But yeah, fine, don't enforce bad habits, like feof, just cause it might work once. Things aren't actually free'd from the stack, the data stays there, and it's safe until another function call overwrites it with it's own args, local variables (or itself). And that just doesn't happen in the program how it is. So yeah, she should probably take your advice into consideration abotu safer buffer handling for the future, but whatever.

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    The problem is arrays in C/C++ are not first class objects. This means you cannot pass an array to a function nor return one. To get around this we have to use various workarounds such as pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM