typedef stuct problem in C

2019-03-06 23:49发布

问题:

I am facing a weird problem I have defined I a structure in a C header file:

typedef struct iRecActive{

    char iRecSID[32];
    unsigned char RecStatus;
    int curSel;

}iRecAcitve_t;

but when I use the same structure in another file, the compiler doesn't recognize the structure even though I have double checked that I have included its header file. Following is the error :

: error C2065: 'iRecActive_t' : undeclared identifier

Following is the full code of the file where I have defined the structure

#ifndef _TS_HTTP_APPLICATION_H_
#define _TS_HTTP_APPLICATION_H_

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct iRecActive{

    char iRecSID[32];
    unsigned char RecStatus;
    int curSel;

}iRecAcitve_t;

int startHTTPServer(int HTMLserverPort);
int closeHTTPServer();

int openTS_SegmenterN();
void pushTSDataN(unsigned char* TSData, int len);
void closeTS_SegmenterN();

void removeAllConnections();

#ifdef __cplusplus
}
#endif

#endif

回答1:

change iRecAcitve_t to iRecActive_t.



回答2:

I tried to find solution for similar problem but I didn't find it on stack. I leave here answer for other people, to save their time:

Because it is C, you cannot create your variables where you want. They must be created at the beginning of statement. So this is correct:

if(true) {
   iRecActive_t myRecActive;
   //calculations
   Mutex_Lock(somemutext);
   variable = 14;

And this is incorrect:

if(true) {
   //calculations
   Mutex_Lock(somemutext);
   variable = 14;
   iRecActive_t myRecActive;

In last example you get error message: illegal use of this type as expression or some other similar, very useful errors.

Proper question is here, but it was marked as duplication (it is not!): typedef stuct problem in C (illegal use of this type as an expression)

Regards, Ikeban