- #1
James889
- 192
- 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:
The printf statement prints garbage.
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:
#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.