82

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?

4
  • Maybe the title of this question should be edited? Can someone more experienced do this if agrees so? Commented Jun 9, 2015 at 13:58
  • You should be able to edit it yourself if you want. But I agree, the title could be better.
    – QuestionC
    Commented Jun 9, 2015 at 15:28
  • @KorayTugay, I took a run at it. Commented Jun 9, 2015 at 16:32
  • 1
    Having a variable declaration inside a case (without surrounding braces as suggested by the top answer) is a bad idea because then the variable's name will be visible in later cases but it will be uninitialized (unless you dropped down).
    – M.M
    Commented Jun 9, 2015 at 23:56

2 Answers 2

111

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.

7
  • 22
    Nice. Nitpicking "Each case ... is ... a labeled-statement". And that's the reason: statements can be labeled but not declarations. Commented Jun 9, 2015 at 8:34
  • 4
    @KorayTugay Sometimes compiler messages aren't informative as we want them to be.... sometimes they are too informative (cough cough C++ stl cough cough).
    – John M
    Commented Jun 9, 2015 at 8:36
  • 5
    @BlueMoon You're right. When you can buy a tool to simplify C++ stl error messages, you know the information density is incredibly low!
    – John M
    Commented Jun 9, 2015 at 9:07
  • 3
    or just move declarations before switch block Commented Jun 9, 2015 at 11:16
  • 3
    @immibis: In C++ declarations are statements, which is why in C++ you can label declarations without any restrictions. In C declarations are not statements, which is why you can't label them. Here's an example that illustrates this difference between C and C++: stackoverflow.com/a/19830820/187690 Commented Jun 10, 2015 at 5:31
15

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

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

Or just use an empty statement.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.