Microsoft KB Archive/64686

= PRB: Nested Nameless Structs Can Cause C2020 Error in C =

Article ID: 64686

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft C Professional Development System 6.0a
 * Microsoft C Professional Development System 6.0a
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Standard Edition
 * Microsoft Visual C++ 6.0 Service Pack 5

-



This article was previously published under Q64686



SYMPTOMS
In Microsoft C, using a nameless structure as a member of a structure causes a compiler error if the two structures both have members with the same name. C versions 6.x generate the error:

C2030: 'varname': struct/union member redefinition.

In C/C++ versions 7.0 and later, the error is:

error C2020: 'varname' : 'struct' member redefinition

The code sample below, when built as a C source file, reproduces the error. The same sample, built as a C++ source file, will not display this behavior.



CAUSE
This is expected behavior, not a bug. When a nameless structure is used within another structure, the members of the nameless structure become members of the new structure. In the C language, the member names must be unique.



Sample Code
/* compile options needed: none /Tc */

void main(void) {

struct s1   { int a,b,c; };

struct s2   { struct s1; /* nameless struct */ float z;     char a[10];  /* error occurs here */ } *p_s2; } The second structure (s2) is effectively the following: struct s2 { int a,b,c; /* Was nameless struct s1 */ float z;   char a[10];  /* Error occurs here */ } *p_s2; This illustrates that the member variable &quot;a&quot; is being redefined. Correctly used nameless structures can provide a good technique for building structures with similar data structure without having the complexity of truly nested structures.

Additional query words: 8.0 8.0c 9.0 10.0 11.0 /Tp

Keywords: kbprb kbcode kbcompiler KB64686

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.