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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    Any example? When you know how everything seems easy, but if you don't well... I know it's a u32 and what the values are but getting it there is another story, at leaset for me. The fact that the 4 bytes need to be in little endian format is another matter but I can deal w/that.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bobv View Post
    Any example? When you know how everything seems easy, but if you don't well... I know it's a u32 and what the values are but getting it there is another story, at leaset for me. The fact that the 4 bytes need to be in little endian format is another matter but I can deal w/that.
    Code:
         // IP converter
    #include <stdio.h>
    #include <string.h>
    
    unsigned long ConvertIP(char *IPString)
      {
        union t_cvt
          { unsigned long ip;
            unsigned char bv[4]; }
        cvt;
    
        sscanf(IPString,"%hhu.%hhu.%hhu.%hhu",&cvt.bv[0],&cvt.bv[1],&cvt.bv[2],&cvt.bv[3]);
    
        // for diagnostic only remove in use
        printf("%u %u %u %u\n\n",cvt.bv[0],cvt.bv[1],cvt.bv[2],cvt.bv[3]);
    
        return cvt.ip; }
    
    
    int main (void)
      { char ipstr[17];
        unsigned int ipaddr;
    
        printf("Enter an IP address : ");
        scanf("%s",ipstr);
     
        ipaddr = ConvertIP(ipstr);
    
        printf("\n %x\n\n",ipaddr);
    
        return 0; }
    To flip between little endian and big endian... simply reverse the assignment order in line 12
    Last edited by CommonTater; 11-22-2011 at 12:29 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    anduril462, Thanks for your reply. I understand your code however this is not the problem I'm trying to solve. I'm trying to pass a string address (pointer) back through a member in the function call. Bob

  4. #4
    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
    }

  5. #5
    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

  6. #6
    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]; 
    }

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