Why is this code compiled without errors? Is this a gcc error?

The code below will compile, although the char * declaration inside the structure does not have a semicolon.

#include<stdio.h>

typedef struct map
{
   int id;
   char *name
}map;

int main()
{
    return 0;
}
+4
source share
2 answers

The syntax for declarations structand unionwith C2011 is:

struct-or-union-specifier:
    struct-or-union identifieropt { struct-declaration-list }
    struct-or-union identifier

struct-or-union:
    struct
    union

struct-declaration-list:
    struct-declaration
    struct-declaration-list struct-declaration

struct-declaration:
    specifier-qualifier-list struct-declarator-listopt ;
    static_assert-declaration

specifier-qualifier-list:
    type-specifier specifier-qualifier-listopt
    type-qualifier specifier-qualifier-listopt

struct-declarator-list:
    struct-declarator
    struct-declarator-list , struct-declarator

struct-declarator:
    declarator
    declaratoropt : constant-expression

static_assert-declaration:
    _Static_assert ( constant-expression , string-literal ) ;

C 2011 Online Draft , 6.7.2.1

This is pretty straightforward - every ad struct-declaration-listmust be completed with ;- there are no permissions to return a semicolon before closing }. If you somehow do not suppress it, gcc should emit diagnostics here.

Edit

Myst - gcc , - ( -Werror - ). 6.7.2.1/8. , - , } struct union, , . gcc ( ), .

Myst ,

struct foo;

struct bar {
  struct foo blah;
};

struct foo , blah; , . struct foo , } , struct foo.

, - , C "" "" - , - :

5.1.1.3

1     ( ), , undefined . . 9)
9). , , , . , . .

, "" "", , . , .

+2

, C11 ( , ), (6.7.2.1, 8):

struct-declaration-list struct-or-union-specifier . struct-declaration-list . struct-declaration-list , , undefined. , }, .

, :

  • ( struct).

  • ( ).

, () , } struct-declaration-list, , node.

, ( ), ( - + ).

( , ) , .

clang gcc , ( c11). , , , .

, } , , , , , .

, , .

, , , , .

, struct-declaration (;), .

, -, , } .

, , , (IMHO) - , " .

-1

Source: https://habr.com/ru/post/1685476/


All Articles