Thread: How do you return a value and pointer from a function?

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yeah, I understood your problem, but gave you an example of passing back values through parameters, so you could extrapolate for your situation. I didn't really want to write your whole solution for you. Note that passing a string (char array) to a function passes a pointer to the first element, so you don't need to do anything else. Treat it like an array in the function. Changing array[3] in the function will change array[3] in the calling code as well:
    Code:
    void parseIP(char *input, char *output)
    {
        ...
        strcpy(output, "foobar");  // this will change the actual string you passed in
    }

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    Tater,

    Good suggestions. I'll try and put something together using these functions.

    Bob

  3. #18
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    Anduril,

    I'm not trying to change the contents of a string or other value. I'm trying to pass back a pointer to the 4th char (&str+4) in the string w/o using the return statement.

    Thanks for your time,
    Bob

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bobv View Post
    Anduril,

    I'm not trying to change the contents of a string or other value. I'm trying to pass back a pointer to the 4th char (&str+4) in the string w/o using the return statement.

    Thanks for your time,
    Bob
    Based Anduril's example in message #16...
    Code:
    void parseIP(char *input, char *output)
    {
       // ...
       output = &input[4]; 
    }

  5. #20
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    Thanks guys I think this is what I was looking for. Touche to both of you!!

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Bob: I guess I got a little confused on what exactly you wanted. If you want to return a pointer, then, extrapolating from my first example, you need to pass a pointer to a pointer (my example in message #16, that Tater quoted, needs a small modification):
    Code:
    void parseIP(char *input, char **output)
    {
        *output = input + 4;  // or whichever quad you left off at
        *output = &input[4];  // this is equivalent to the above
    }
    ...
    char *input;
    char *next_spot_to_parse;
    parseIP(input, &next_spot_to_parse);
    // now next_spot_to_parse points to the next quad
    // you can pass that in as your new starting point for the next time:
    parseIP(next_spot_to_parse, &next_spot_to_parse);  // this works, since the first param is copied when passed in, so there is no conflict

  7. #22
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    Anduril,
    Most Excellent! I tried your first example "input+4" but didn't use a "**" I just used a single '*', no wonder it didn't work. What you say makes perfect sense too I needed to specify a ptr to a ptr. Many thanks for the clarification, I'd still be spinning my wheels w/o it.

    Tater,
    Your help was awesome too. You made me rethink what I'm doing and am going to process a quadrant at a time at the user character input level which will give me more control and a much cleaner solution. If they bomb on any quadrant I'll send them back to the beginning and force them to start over and I wont let them enter more than 4 quadrants.

    You're both slick coders, hat's off!!

    Forever Grateful,
    Very Rusty C Coder - Bob

  8. #23
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by bobv View Post
    I know how to use return to pass back a value and/or a pointer but would like to do both.
    Like this :

    Code:
    //EX:  Not sure I declared ptrStrgPlus4 correctly to have it be
    // a pointer to strg+4 (ie '.') on return from the getBoth function
    int num;  char strg[5]="123.456"; char *ptrStrgPlus4; 
    
       x = getBoth(10, strg, &ptrStrgPlus4);
    
    // Would like to return the num*10, plus a pointer to the '.'
    int function getBoth(int num, char *string, char **stringPtr) {
        // I know the next line doesn't work
          *stringPtr = string+4;     // Return pointer to 4th char in the string
         return (num*10);
    }
    Edit - should have read to the end of the thread, looks like I'm a day late.

  9. #24
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    KC,

    Great, better late than never....another winner!!!
    So simple when you know how. I was just a couple chars away.
    Hate when that happens.

    Many thanks; have a great Thanksgiving,
    Bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function syntax to return a pointer...
    By tzuch in forum C Programming
    Replies: 1
    Last Post: 05-29-2008, 07:53 AM
  2. Have function return file pointer
    By krogz in forum C Programming
    Replies: 6
    Last Post: 05-03-2007, 08:56 PM
  3. how to make a function return a pointer
    By *DEAD* in forum C Programming
    Replies: 6
    Last Post: 01-14-2007, 07:14 PM
  4. Can a function return a pointer to itself?
    By King Mir in forum C Programming
    Replies: 4
    Last Post: 04-19-2006, 01:15 PM
  5. Function to return pointer to structure
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 06:10 AM