char return value

This is a discussion on char return value within the C Programming forums, part of the General Programming Boards category; hi, i am trying to create a function which processes an answer to a string, the return value of the ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    char return value

    hi,

    i am trying to create a function which processes an answer to a string, the return value of the function response should be a char array containing the answer.

    here is my code so far:

    in main:

    Code:
    	char message = "HALLO";
    	char *getanswer = response(message);
    Code:
    char *response(char message[])
    {
    	char *returnanswer = "HUHU";
    	return returnanswer;
    }
    i have already read some tutorials but i think i got it wrong :-(.

    looking forwart to your answers!



    thx

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    message needs to be a char array, right?
    char message[ ] = "HALLO";

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by calmar View Post
    hi,
    i am trying to create a function which processes an answer to a string, the return value of the function response should be a char array containing the answer.
    here is my code so far:
    in main:
    Code:
    	
    char message = "HALLO";
    char *getanswer = response(message);
    Code:
    char *response(char message[])
    {
       char *returnanswer = "HUHU";
       return returnanswer;
    }
    i have already read some tutorials but i think i got it wrong :-(.
    looking forwart to your answers!

    thx
    Ok, at the risk of being the bearer of bad news... C is a language without a "string" data type and without automated memory management. When we want to work with text we have to create buffers --character arrays-- to hold the text. It becomes a string only because the last (hidden) character is a 0. These buffers are then manipulated by function calls in the string library provided as part of standard C...

    1) your first string char message... is not a string buffer. It has space only for a single character. If you want it to hold more than that you must make it an array...
    Code:
    char message[20] = {"Hallo"};
    As already suggested you can make it "autosize" by using char message[] = "Hallo".

    However, you need to know that passing char* pointers around and assigning them across the equals sign works only with string literals. This because the string (eg. "HuHu" in your code) is stored as part of the program image that is loaded into memory.

    Any time you are working with user entered data --as in "Please enter your name"-- you will need to adopt an entirely different strategy to deal with strings using functions from the standard C library. You will have to create buffer arrays and you will (most often) need to clean them up when you're done with them.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Quote Originally Posted by calmar View Post
    hi,

    i am trying to create a function which processes an answer to a string, the return value of the function response should be a char array containing the answer.
    There is no way to pass a character array back as a return value. Best way to do what I think you are trying to do is to create the character array in the parent function and pass the pointer to the subfunction, letting the subfunction modify it from there.

    Code:
    int response(char *message, char *response)
    {
         strcpy(*response, "HUHU");
         return 0;
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Phane View Post
    There is no way to pass a character array back as a return value. Best way to do what I think you are trying to do is to create the character array in the parent function and pass the pointer to the subfunction, letting the subfunction modify it from there.

    Code:
    int response(char *message, char *response)
    {
         strcpy(*response, "HUHU");
         return 0;
    }
    1) Don't pass in unnecessary parameters
    2) For functions that return nothing useful use void
    3) Don't name variables and functions with the same name.

    Code:
    void response(char *Response)
    {
         strcpy(Response, "HUHU");
    }
    Last edited by CommonTater; 04-01-2011 at 08:39 AM.

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    4) Don't name things that differ only by case. It leads to stuff like this, and yes they both do different things
    Attached Images Attached Images  

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    'response' and 'Response' ARE different names.

    Quote Originally Posted by Phane View Post
    There is no way to pass a character array back as a return value.
    Not strictly true. You could embed the array in a struct and pass it back.

    It is often overlooked that a struct, no matter how large, is treated as a primary data type. It can be used in assignment (a whopping and FAST memory move!!) or in inequality testing, as far as I know. I have to test that... but hey, sounds reasonable. Not to mention structs can be passed by value and returned likewise.

    Oh, sorry, adeyblue. I didn't read your response carefully. You are saying even different case causes problems? The screen display means nothing to me. Can you elaborate?
    Last edited by nonoob; 04-01-2011 at 12:54 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    'response' and 'Response' ARE different names.
    As far as the compiler is concerned, yes they are.

    Oh, sorry, adeyblue. I didn't read your response carefully. You are saying even different case causes problems? The screen display means nothing to me. Can you elaborate?
    The image is showing two functions with extremely similar names... never a good idea. Miss a pinky hold on the shift key and you get totally incorrect behavior.

    His point is valid as a matter of practice... but the compiler does know them apart.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic optimization request...
    By feeder74 in forum C++ Programming
    Replies: 27
    Last Post: 05-05-2010, 07:17 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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