Switch Fails After Commenting on an Unused String

Here is my code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> int main (void) { struct addrinfo hints; memset (&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_CANONNAME; struct addrinfo *res; getaddrinfo ("example.com", "http", &hints, &res); printf ("Host: %s\n", "example.com"); void *ptr; while (res != NULL) { printf("AI Family for current addrinfo: %i\n", res->ai_family); switch (res->ai_family) { case AF_INET: ptr = (struct sockaddr_in *) res->ai_addr; struct sockaddr_in *sockAddrIn = (struct sockaddr_in *) res->ai_addr; break; } res = res->ai_next; } return 0; } 

which compiles fine.

However, when I comment on this line:

 //ptr = (struct sockaddr_in *) res->ai_addr; 

I will get:

 $ gcc ex4.c ex4.c:30:9: error: expected expression struct sockaddr_in *sockAddrIn = (struct sockaddr_in *) res->ai_addr; ^ 1 error generated. 

What am I missing?

+80
c
Jun 09 '15 at 8:23
source share
2 answers

Each case in a switch statement, technically speaking, is a label. For some obscure and old reasons, you are not allowed to have a variable declaration as the first line after the label. Commenting on an appointment

 ptr = (struct sockaddr_in *) res->ai_addr; 

line

 struct sockaddr_in *sockAddrIn = (struct sockaddr_in *) res->ai_addr; 

becomes the first line after the AF_INET: label, which, as I said, is illegal in C.

The solution is to wrap all your arguments in braces like this:

 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> int main (void) { struct addrinfo hints; memset (&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_CANONNAME; struct addrinfo *res; getaddrinfo ("example.com", "http", &hints, &res); printf ("Host: %s\n", "example.com"); void *ptr; while (res != NULL) { printf("AI Family for current addrinfo: %i\n", res->ai_family); switch (res->ai_family) { case AF_INET: { ptr = (struct sockaddr_in *) res->ai_addr; struct sockaddr_in *sockAddrIn = (struct sockaddr_in *) res->ai_addr; break; } } res = res->ai_next; } return 0; } 

Anyway, I think this is a better coding practice.

+109
Jun 09 '15 at 8:29
source share

As a complement to the accepted answer, you can declare your variables before case labels.

 switch(a) { int b; //can't initialize variable here case 0: ... } 

Or just use an empty statement.

+15
Jun 10 '15 at 2:46
source share



All Articles