Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C newbie question returntype

  1. Dec 28, 2011 #1
    Hi,

    I am trying to get my head around this problem.

    The task is to write a function that takes a pointer to a struct and simply print the address filled in. My problem is that the function does not return the string properly and i am not sure why. Printing it from within the function works.



    Here is my attempt:

    Code (Text):

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    char *printInet(struct sockaddr_in *my_struct) {
            char Buffer[32];
            char *address = inet_ntop(AF_INET,&(my_struct->sin_addr),Buffer,32);
            printf("%s\n",address);
            return address;
    }


    int main(int argc, char *argv[]) {


    struct sockaddr_in socket, *socket_ptr;
                       socket.sin_family = AF_INET;
                       socket.sin_addr.s_addr = inet_addr("12.12.12.12");
                       socket.sin_port = htons(80);


    socket_ptr = &socket;

    printf("%s",printInet(socket_ptr));

    return 0;
    }
     
    The printf statement prints garbage.
     
  2. jcsd
  3. Dec 28, 2011 #2

    rcgldr

    User Avatar
    Homework Helper

    The character pointer "address" and the array "buffer"in "printInet()", are local variables, they reside on the stack. When printInet() returns, the stack pointer is set back to what it was in "main()", and the local variables in printInet(), may get overwritten (which is apparently what happened in this case).

    You can declare "address" and "buffer" as static, so that they are permanent, but with local names. You could also move them outside all functions, but then they would be a global variables. You could also allocate a buffer in printInet, then return the poiner to that buffer, and have the main code free that buffer after printing it out.

    update - as phiby points out in the next post, only Buffer needs to be static. printInet() returns the value stored in address so there's no point in making address static.
     
    Last edited: Dec 28, 2011
  4. Dec 28, 2011 #3
    Buffer needs to be static. address doesn't.
     
  5. Dec 28, 2011 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Even better in my opinion would be to add the output buffer as an argument to printInet, similar to how inet_ntop works.


    There's an error in this code. The function inet_ntop will return a null pointer to indicate a problem with the provided pointer to an internet address structure. Print this null pointer and you'll get a core dump. Your code should protect against the potential dereferencing of a null pointer by printf.
     
  6. Jan 2, 2012 #5
    Thanks for the valuable input.

    Just one last follow up question.
    If i wanted to build a simple chat program in C++, can i use the same library functions that i did in C?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C newbie question returntype
Loading...