UNIX Socket Programming in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NarutoUzumaki
    New Member
    • Aug 2019
    • 1

    UNIX Socket Programming in C

    Can someone please explain me what does (struct sockaddr*)&serv eraddr means in the CONNECT system call. Reading this kind of syntax for the first time. Thanks.
    Attached Files
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    "(struct sockaddr*)&serv eraddr"

    Explicit type casting is done here. serveraddr is an object of structure sockaddr_in, but my guess is that the second actual parameter of connect() requires an address of type sockaddr and holds the address in a sockaddr type pointer (via formal arguments).

    This is similar to what is done with primitive data types like int, float etc. And structure is also a data type (derived one). For example, while dynamically allocating memory,

    Code:
    int *ptr = (int *) malloc(sizeof(int) * length);
    malloc() returns a void* type value, that's why explicit type conversion is done to convert it to int* type and then assign it to an int type pointer. However, in this particular case it is unnecessary since void* is automatically promoted to any other pointer type.

    Comment

    Working...