I need to write a function that creates an instance of SOCKADDR_IN. But in this function, I somehow need to transmit the IP address. How to do it? Array char?
sockaddr_in * addresinit (?? ip)
{
SOCKADDR_IN ADDR;
int sizeofaddr = sizeof (addr);
Addr.sin_addr.s_addr = inet_addr (IP);
Addr.sin_port = HTNS (1111);
addr.sin_family = af_inet;
}
Answer 1, Authority 100%
inet_addr
features char *
argument, which is a pointer to a zero-terminated string containing the string representation of the IP address.
More in documentation .
Your code can be supplemented like this:
sockaddr_in & amp; addresinit (const char * ip)
{
SOCKADDR_IN ADDR;
int sizeofaddr = sizeof (addr);
Addr.sin_addr.s_addr = inet_addr (IP);
Addr.sin_port = HTNS (1111);
addr.sin_family = af_inet;
}
Const
Before Char *
In an ad type argument type, it means that you are not going to change its value in the body of this function.
Use something like this:
sockaddr_in & amp; SCKADDR = AddResinit ("127.0.0.1"); // Lock address is taken only for example, any IP
At the request of the suffering adds some more fixes to your code:
sockaddr_in addresinit (const char * ip)
{
SOCKADDR_IN ADDR;
// (removed, because it is not used) int Sizeofaddr = Sizeof (ADDR);
Addr.sin_addr.s_addr = inet_addr (IP);
Addr.sin_port = HTNS (1111);
addr.sin_family = af_inet;
RETURN ADDR;
}
The resentment of the community was caused by the fact that you returned the link to the variable created in the body body. When you exit the function, this variable will be deleted, and the link will indicate the “empty place”, which can lead to poor consequences.