注释掉未使用的行后,开关案例将无法编译

时间:2015-06-09 08:23:41

标签: c

这是我的代码:

#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;
}
编译好的

然而,当我注释掉这一行时:

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

我会得到:

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

我错过了什么?

2 个答案:

答案 0 :(得分:111)

从技术上讲,switch语句中的每个案例都是一个标签。对于某些obscure and old reasons,不允许将变量声明作为标签后的第一行。通过评论作业

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

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

成为标签AF_INET:之后的第一行,就像我说的那样,在C中是非法的。

解决方案是将所有case语句包装在大括号中,如下所示:

#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;
}

无论如何,我认为这是更好的编码实践。

答案 1 :(得分:15)

作为已接受答案的补充,您可以在案例标签之前声明变量。

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

或者只使用空语句。