Register to reply

C newbie question returntype

by James889
Tags: newbie, returntype
Share this thread:
James889
#1
Dec28-11, 06:02 AM
P: 184
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:

#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.
Phys.Org News Partner Science news on Phys.org
Mysterious source of ozone-depleting chemical baffles NASA
Water leads to chemical that gunks up biofuels production
How lizards regenerate their tails: Researchers discover genetic 'recipe'
rcgldr
#2
Dec28-11, 06:45 AM
HW Helper
P: 7,108
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.
phiby
#3
Dec28-11, 07:30 AM
P: 77
Quote Quote by rcgldr View Post
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.
Buffer needs to be static. address doesn't.

D H
#4
Dec28-11, 07:59 AM
Mentor
P: 15,149
C newbie question returntype

Quote Quote by phiby View Post
Buffer needs to be static. address doesn't.
Even better in my opinion would be to add the output buffer as an argument to printInet, similar to how inet_ntop works.


Quote Quote by James889 View Post
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;
}
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.
James889
#5
Jan2-12, 02:05 PM
P: 184
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?


Register to reply

Related Discussions
Newbie Question Electrical Engineering 2
Newbie question (QM) Advanced Physics Homework 2
Newbie question Differential Equations 1
Another newbie question Astronomy & Astrophysics 4
Newbie Question Introductory Physics Homework 1