What are the basics of building a chat program in C++?

  • Thread starter James889
  • Start date
In summary, You are trying to print out an internet address using the inet_ntop function, but you get an error. You should protect against potential dereferencing of a null pointer with printf.
  • #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:

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.
 
Technology news on Phys.org
  • #2
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:
  • #3
rcgldr said:
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.
 
  • #4
phiby said:
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.
James889 said:
Code:
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.
 
  • #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?
 

What is a "C newbie question returntype"?

A "C newbie question returntype" refers to a question asked by someone who is new to the programming language C, specifically related to the return type of a function or method.

Why is understanding return types important in C?

Understanding return types is important in C because it allows you to know what type of data a function will return. This is important for using and manipulating the data correctly in your program.

What are the different types of return types in C?

There are several types of return types in C, including void (no return value), int (integer), char (character), float (floating point number), double (double-precision floating point number), and more. The specific return type used depends on the function's purpose and what data it needs to return.

How do I determine the return type of a function in C?

The return type of a function can typically be found in the function's declaration or in the function's documentation. It is usually specified before the function's name and is denoted by a data type, such as int or char.

Can I change the return type of a function in C?

No, the return type of a function cannot be changed. It is determined by the function's definition and cannot be modified without altering the function's purpose and functionality.

Similar threads

  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Computing and Technology
Replies
13
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
75K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top