在VS 2010下编译C代码

compiling C code under VS 2010

本文关键字:代码 编译 2010下 VS      更新时间:2023-10-16

我试图在MS VS 2010下编译旧的C代码。错误发生在结构体声明和调用。

声明(编辑):

typedef struct tStr
{
int nInt;
int ***anPoint;
};

用法:

struct tStr tStuff;
tStuff.nInt = 0;

函数声明(编辑:现在可能有效):

int doStuff(struct tStr *sStuff, int nStuff);

编译器报错未识别的标识符,缺少(或;或者{等等。它们都被归类为语法错误。我检查的语法应该没问题。所以我在我的结束…

我重新做了结构声明。但还是同样的错误:

error C2143: syntax error : missing ';' before 'type'

好,回到原来的声明。但是,如果我尝试像使用时那样访问结构变量,我会得到一个错误:

error C2065: 'tStuff' : undeclared identifier

所以我的代码是:

typedef struct tMatrix
{
int nRows;
int nCols;
int nVec;
int ***anMatrix;
};
int allocateMatrix(struct tMatrix *sMatrix, int nType);
struct tMatrix sMatrix1;
sMatrix1.nRows = 0;

错误:

error C2143: syntax error : missing ';' before 'type'
error C2065: 'sMatrix1' : undeclared identifier

任何想法?

谢谢你的回答,我想我还是重写一遍吧。我还遇到了更多关于类型转换的错误。为了避免头痛,我就重新开始。再次感谢。

简短的回答是您缺少typedef的一个参数(继续获得完整的解释)。

typedef的语法为:

typepedef 类型定义标识符;

在您的示例中,类型定义是struct tStr,并且缺少标识符。

如果你想从你的结构中创建一个新的类型定义,你可以这样声明它:

typedef struct tStr_ {
    int nInt;
    int ***anPoint;
} tStr;

现在您可以在代码中引用新类型tStr:

tStr tStuff;
tStuff.nInt = 0;

在本例中,tStr_是结构体的名称,tStr是新类型的名称。您仍然可以通过其名称引用该结构体:

struct tStr_ tStuff;
tStuff.nInt = 0;
编辑:也许我们需要一个更完整的例子来说明你想要完成的任务。以下代码示例编译时没有出现错误,并产生预期的结果:
#include <stdio.h>
#include <stdlib.h>
typedef struct tMatrix_ {
    int nRows;
    int nCols;
    int nVec;
    int ***anMatrix;
} tMatrix;
int allocateMatrix(tMatrix *sMatrix, int nType) 
{
    sMatrix->nRows = 10;
    return 0;
}
int main(void)
{       
    tMatrix sMatrix1;
    allocateMatrix(&sMatrix1, 0);
    printf("%dn", sMatrix1.nRows);
    return EXIT_SUCCESS;
}

除非你在C中定义了一个结构体,否则用法应该是:

struct TStr tStuff;

您是否在项目文件中使用/Tc命令行参数?http://msdn.microsoft.com/en-us/library/032xwy55.aspx