typedef stuct problem in C

不想你离开。 提交于 2019-12-20 07:40:03

问题


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



来源:https://stackoverflow.com/questions/3385533/typedef-stuct-problem-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!