Does interception intercept in C? If so, is this supposed to work in VisualStudio 2003?

I hate defines. In search of eliminating as much of the old code base as possible, I need to rely on enum-hack to determine the structures containing arrays. Unfortunately, I am not going to compile it when the incriminated .h file is included in C projects.

typedef struct _P_O { enum { MAXELEMENTS=10 }; P_O_U elem; ULONG id[MAXELEMENTS]; USHORT nid; } P_O, *PP_O; 

this leads to error C2208 :

'type': no ​​members defined using this type
An identifier that resolves the type name is in the aggregate declaration, but the compiler cannot declare the participant.

OT: I hate using the compiler, which is 10 yo, in order to use the old c code and poor design; not only determines :)

+4
source share
2 answers

Given the code in the question, GCC warns: warning: declaration does not declare anything for an enumeration string.

Put the listing outside the structure and everything will be fine.

 typedef int P_O_U; typedef unsigned long ULONG; typedef unsigned short USHORT; enum { MAXELEMENTS = 10 }; typedef struct _P_O { P_O_U elem; ULONG id[MAXELEMENTS]; USHORT nid; } P_O, *PP_O; 
+6
source
 struct foo { enum { MAXELEMENTS=10 }; long id[MAXELEMENTS]; }; 

enum hack, like this, acts in C (but not a good code style, see @Jonathan Leffler's comment). Because when id defined, MAXELEMENTS is qualified with an enumeration constant.

C99 6.2.1 Identifier areas

Elements of structure, union, and enumeration have an area that begins immediately after the appearance of the tag in the type specifier that declares the tag. Each enumeration constant has an area that begins immediately after the appearance of its defining enumerator in the list of enumerations. Any other identifier has a scope that begins immediately after the completion of its declaration.

And enumeration is a type of integer constant.

C99 6.6 Constant Expressions

An integer constant expression must be an integer type and must have only operands that are integer constants, enumeration constants, symbolic constants, sizeof expressions, the results of which are integer constants, and floating-point constants, which are direct operands of the cast. Cast operators in an integer constant expression must convert only arithmetic types to integer types, except that they are part of the operand for the sizeof operator.

Finally, an integer constant can be used in array declarations, see C99. 6.7.5.2. Manifest declaration. No wonder.

+3
source

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


All Articles