Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to cast struct to another?

Any one could describe how (struct sockaddr *)&server works here? Is it possible to cast bigger struct to smaller struct?

See these structs:

// IPv4 AF_INET sockets:
struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};

struct sockaddr {
    unsigned short    sa_family;    // address family, AF_xxx
    char              sa_data[14];  // 14 bytes of protocol address
};

This is the main program:

int main(int argc , char *argv[])
 {
        int socket_desc;
        struct sockaddr_in server;

    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }

    server.sin_addr.s_addr = inet_addr("74.125.235.20");
    server.sin_family = AF_INET;
    server.sin_port = htons( 80 );

    //Connect to remote server
    if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected");
    return 0;
}
like image 945
Milad Khajavi Avatar asked Jun 12 '12 14:06

Milad Khajavi


People also ask

Can I assign struct to another?

Yes, you can assign one instance of a struct to another using a simple assignment statement. In the case of non-pointer or non pointer containing struct members, assignment means copy. In the case of pointer struct members, assignment means pointer will point to the same address of the other pointer.

Can you inherit from a struct C?

Structs do not support inheritance, but they can implement interfaces.

Can struct be copied?

A struct variable in Golang can be copied to another variable easily using the assignment statement(=). Any changes made to the second struct will not be reflected back to the first struct.

Can structs hold other structs?

C Nested StructureOne structure can be declared inside other structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data.


2 Answers

This is refered as Type Punning. Here, both structures have the same size, so there is no question of struct size. Although you can cast almost anything to anything, doing it with structures is error-prone.

like image 148
Benoit Avatar answered Oct 07 '22 11:10

Benoit


This is C's form of "inheritance" (notice the quotes). This works because C does not care about the underlying data in an address, just what you represent it as.

The function determines what structure it actually is by using the sa_family field, and casting it into the proper sockaddr_in inside the function.

like image 27
Richard J. Ross III Avatar answered Oct 07 '22 10:10

Richard J. Ross III