C99 struct initialiser with embedded self assignment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello GCC community!

I recently spent much time trying to figure out a bug in my code, that I feel the compiler should have warned me about. I'm also not so sure what the C99 standard mandates as proper behaviour regarding this, so I'd appreciate your expertise.

Without further ado, here is a small program that reproduces my issue - the bug in my code was the mistake shown in "query2", the rest are only for comparison:


#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

int main(void)
{
        struct addrinfo query1 = {
                .ai_family = AF_UNSPEC,
                .ai_socktype = SOCK_STREAM,
                .ai_flags = AI_PASSIVE
        };
        struct addrinfo query2 = {
                .ai_family = AF_UNSPEC,
                .ai_socktype = SOCK_STREAM,
                query2.ai_flags = AI_PASSIVE
        };
        struct addrinfo query3 = {
                .ai_family = AF_UNSPEC,
                .ai_socktype = SOCK_STREAM,
		1
        };

        printf("query1.ai_flags:    %d, query2.ai_flags:    %d, "
	       "query3.ai_flags:    %d\n"
	       "query1.ai_protocol: %d, query2.ai_protocol: %d, "
	       "query3.ai_protocol: %d\n",
               query1.ai_flags, query2.ai_flags, query3.ai_flags,
	       query1.ai_protocol, query2.ai_protocol, query3.ai_protocol);

        return 0;
}


The output of this program is:

query1.ai_flags:    1, query2.ai_flags:    1, query3.ai_flags:    0
query1.ai_protocol: 0, query2.ai_protocol: 1, query3.ai_protocol: 1


For reference here is struct addrinfo:

struct addrinfo {
    int ai_flags;
    int ai_family;
    int ai_socktype;
    int ai_protocol;
    socklen_t ai_addrlen;
    struct sockaddr *ai_addr;
    char *ai_canonname;
    struct addrinfo *ai_next;
}



Do you think that the compiler should have warned in query2? Something like "suggest parentheses around assignment"?

What is the standard behaviour for such an assignment? For query2, I would have expected that the outer assignment is applied after the inner, thus turning query2.ai_flags to 0 (because of zeroing the whole struct).


Thank you in advance,
Dimitris





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux