C ++: Why casting as a pointer and then divulging the work?

I recently worked on sockets in C ++, and I came across this:

*(struct in_addr*)&serv_addr.sin_addr.s_addr = *(struct in_addr *)server->h_addr; 

While this does what I want, I am a little confused why I cannot do this:

 (struct in_addr)serv_addr.sin_addr.s_addr = *(struct in_addr *)server->h_addr; 

Since it becomes a pointer, and then immediately dereferenced, you should not do the second job, as well as the first? I'm still new to C ++ and this is a bit confusing to me. Any help would be greatly appreciated. Below is the code. All it does is the host name or IP address and prints the IP address on the screen.

 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> using namespace std; int main(){ int socketfd, portno, rwResult; struct sockaddr_in serv_addr; struct hostent* server; char inputName[50]; //The next block gets the host name and port number and stores them in variables cout<<"Enter host(Max Size 50): "; cin>>inputName; cout<<endl<<"Enter port number: "; cin>>portno; cout<<endl; server = gethostbyname(inputName); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(portno); *(struct in_addr*)&serv_addr.sin_addr.s_addr = *(struct in_addr *)server->h_addr; //This is where I am confused //(struct in_addr)serv_addr.sin_addr.s_addr = *(struct in_addr *)server->h_addr; cout<< "Server: "<<inet_ntoa(*(struct in_addr *)server->h_addr_list[0])<<endl; cout<< "Server: "<<inet_ntoa(*(struct in_addr *)&serv_addr.sin_addr.s_addr)<<endl; //The two cout tell me if the address was copied correctly to serv_addr I believe. return 0; } 
+4
source share
3 answers

A simpler example might help explain the difference:

 double q = 0.5; int n; n = (int) q; // #1 n = *(int*)(&q) // #2 

The first version converts the q value to an int value in accordance with the rules of the language. Conversion is possible only for primitive types and for classes that define conversion operators / constructors.

The second version reinterprets the binary representation of q as an integer. In good cases (for example, in your example) this creates something useful, but in general this behavior is undefined, since it is not allowed to access a variable of one type using a pointer to another type.

Your example may be valid because the two types in question are POD structures with the same initial elements, and you only get access to one of the common source elements. Edit. I checked, server->h_addr is of type char * . Perhaps this just serves as a placeholder, and the pointer is actually a structure of the correct type.

+3
source

Objects can only be passed as another class if there is a constructor that takes an argument of the original type.

Pointers, on the other hand, can be entered by will or not. However, this can be very dangerous. If you need to do this, use static_cast or dynamic_cast for this, and perhaps check if the translation was successful. Another stylistic advantage in this more explicit way is that it makes the cast more explicit for someone looking at your code.

+4
source

What the code does is try to re-interpret serv_addr.sin_addr.s_addr as type in_addr . However, this conversion is incompatible. Therefore, when you try to broadcast live you get a compiler error in the second fragment.

In the first snippet, you essentially use casting to get around this incompatibility.

+3
source

All Articles