Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch-case won't compile after commenting out an unused line

Tags:

c

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 out 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?

like image 775
Koray Tugay Avatar asked Jun 09 '15 08:06

Koray Tugay


People also ask

What happens if you don't break switch statement?

Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached. This continuation may be desirable in some situations. The default statement is executed if no case constant-expression value is equal to the value of expression .

What if none of the cases match the value in switch-case statement?

The computer will go through the switch statement and check for strict equality === between the case and expression . If one of the cases matches the expression , then the code inside that case clause will execute. If none of the cases match the expression, then the default clause will be executed.

What would be the consequence of omitting the default case of the switch statement?

If there is no default: label and none of the case labels match the "switched" value, then none of the controlled compound statement will be executed. Execution will continue from the end of the switch statement.

Can a switch-case have multiple lines?

C static code analysis: "switch case" clauses should not have too many lines of code.


1 Answers

Each case in a switch statement is, technically speaking, a label. For some obscure and old reasons, you are not allowed to have a variable declaration as the first line after a label. By commenting out the assignment

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

the line

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

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

The solution is to wrap all of your case statements in curly brackets like so:

#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 better coding practice.

like image 121
John M Avatar answered Oct 05 '22 06:10

John M