Differences between struct in C and C++
Asked Answered
Y

5

15

I am trying to convert a C++ struct to C but keep getting "undeclared identifier"? Does C++ have a different syntax for referring to structs?

struct KEY_STATE 
{
    bool kSHIFT; //if the shift key is pressed 
    bool kCAPSLOCK; //if the caps lock key is pressed down
    bool kCTRL; //if the control key is pressed down
    bool kALT; //if the alt key is pressed down
};

I am using a variable of type KEY_STATE inside another structure:

typedef struct _DEVICE_EXTENSION
{
    WDFDEVICE WdfDevice;
    KEY_STATE kState;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;

results in error C2061: syntax error : identifier 'KEY_STATE'

...on the line KEY_STATE kState; I am building with the WDK compiler if that makes any difference. This is in a header file of course. I am porting C++ WDM driver to WDF and C.

This is the MSDN article for C2061.

An initializer may be enclosed by parentheses. To avoid this problem, enclose the declarator in parentheses or make it a typedef.

This error could also be caused when the compiler detects an expression as a class template argument; use typename to tell the compiler it is a type.

Changing KEY_STATE to typedef struct still causes this error and actually causes a lot more. There are no free parentheses or things in too many parentheses, that is the other thing the article suggests.

Yepez answered 29/9, 2009 at 13:59 Comment(0)
R
30

In C, the name of the type is struct KEY_STATE.

So you have to declare the second struct as

typedef struct _DEVICE_EXTENSION
{
    WDFDEVICE WdfDevice;
    struct KEY_STATE kState;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;

If you do not want to write struct all the time, you can use a typedef declare KEY_STATE similar to DEVICE_EXTENSION:

typedef struct _KEY_STATE
{
    /* ... */
} KEY_STATE;
Reckon answered 29/9, 2009 at 14:2 Comment(0)
W
16

There is no bool type in C prior to C99.

Also, there is no type called KEY_STATE when you do struct KEY_STATE.

Try this instead:

typedef struct _KEY_STATE 
{
    unsigned kSHIFT : 1; //if the shift key is pressed 
    unsigned kCAPSLOCK : 1; //if the caps lock key is pressed down
    unsigned kCTRL : 1; //if the control key is pressed down
    unsigned kALT : 1; //if the alt key is pressed down
} KEY_STATE;
Wenz answered 29/9, 2009 at 14:10 Comment(1)
+1 thanks, the bool was causing a bunch more errors I didn't really understandYepez
C
6

You need to refer to KEY_STATE with struct KEY_STATE. In C++ you can leave the struct out, but not in plain C.

Another solution is to do a type alias:

typedef struct KEY_STATE KEY_STATE

Now KEY_STATE means the same thing as struct KEY_STATE

Chaplain answered 29/9, 2009 at 14:2 Comment(0)
B
5

You could/should typedef the struct so that you don't need the struct keyword every time you will declare a variable of that type.

typedef struct _KEY_STATE 
{
    bool kSHIFT; //if the shift key is pressed 
    bool kCAPSLOCK; //if the caps lock key is pressed down
    bool kCTRL; //if the control key is pressed down
    bool kALT; //if the alt key is pressed down
} KEY_STATE;

Now you can do:

KEY_STATE kState;

or (as in the example you have) :

struct KEY_STATE kState;
Brittle answered 29/9, 2009 at 14:5 Comment(0)
D
4

You have to qualify a struct variable with the 'struct' keyword:

typedef struct _DEVICE_EXTENSION
{
    WDFDEVICE WdfDevice;
    struct KEY_STATE kState;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;

When you use DEVICE_EXTENSION you don't have to do use 'struct' because you are doing a struct definition and a typedef in a single compound statement. So you could do the same for KEY_STATE if you wanted to use it in a similar fasion:

typedef struct _KEY_STATE_t
{
    bool kSHIFT; //if the shift key is pressed 
    bool kCAPSLOCK; //if the caps lock key is pressed down
    bool kCTRL; //if the control key is pressed down
    bool kALT; //if the alt key is pressed down
} KEY_STATE;
Detritus answered 29/9, 2009 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.