What does this point in the structure code mean?

Asked

Viewed 52 times

3

int main(){
    char buff[129];
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 0), &wsa);

    struct sockaddr_in caddr;
    struct sockaddr_in saddr = {
        .sin_family      = AF_INET,
        .sin_addr.s_addr = htonl(INADDR_ANY),
        .sin_port        = htons(5000)
    };
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

3

In this context it is a way to indicate which members are to be used. It ends up serving to disambiguate whether that is a normal variable or a member of the structure. Without the point it could be just a variable. It’s an abbreviated form of writing like this:

struct sockaddr_in saddr = {
    saddr.sin_family      = AF_INET,
    saddr.sin_addr.s_addr = htonl(INADDR_ANY),
    saddr.sin_port        = htons(5000)
};

These names after the point are the members declared in the structure at some other point in the code, and what is before the point is the object.

Another way that works:

struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(5000);

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.