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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    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.

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