C语言struct的问题

InfidelX 2009-10-16 01:32:17
Gcc下编译没有问题,但是到了vc2005,编译不过……
兄弟们帮我看看什么问题……

源代码如下:
#include "stdio.h"

struct testStruct
{
int aInt;
double aDouble;
};

typedef struct testStruct TestStruct;
typedef struct testStruct* pTestStruct;

main()
{
struct testStruct test;
test.aInt = 100;
test.aDouble = 10000.f;
printf("%d\n%f\n",test.aInt,test.aDouble);

pTestStruct structArray[100];

structArray[0] = (pTestStruct)malloc(sizeof(TestStruct));
structArray[0]->aInt = 100000;
structArray[0]->aDouble = 9998.2121f;

printf("%d\n%f\n",structArray[0]->aInt,structArray[0]->aDouble);
free(structArray[0]);
}

output窗口输出,怎么说pTestStruct使用不合法?
1>------ Build started: Project: CStruct, Configuration: Debug Win32 ------
1>Compiling...
1>test.c
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(20) : error C2275: 'pTestStruct' : illegal use of this type as an expression
1> f:\solutions - windowsprogramming\cstruct\cstruct\test.c(10) : see declaration of 'pTestStruct'
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(20) : error C2146: syntax error : missing ';' before identifier 'structArray'
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(20) : error C2065: 'structArray' : undeclared identifier
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(20) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(22) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(22) : warning C4013: 'malloc' undefined; assuming extern returning int
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(22) : warning C4312: 'type cast' : conversion from 'int' to 'pTestStruct' of greater size
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(23) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(24) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(27) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(27) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(28) : warning C4013: 'free' undefined; assuming extern returning int
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(28) : error C2109: subscript requires array or pointer type
1>Build log was saved at "file://f:\Solutions - WindowsProgramming\CStruct\CStruct\Debug\BuildLog.htm"
1>CStruct - 10 error(s), 3 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
...全文
358 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuiandk 2009-10-18
  • 打赏
  • 举报
回复
9 楼正解
gcc 编译C代码时,对变量的定义位置没有要求
但是VS需要定义变量在函数的开头
suck_kcusallen 2009-10-17
  • 打赏
  • 举报
回复
都是强人
Urselect 2009-10-17
  • 打赏
  • 举报
回复
我也不知道原来还有这样的事情,呵呵
[Quote=引用 10 楼 jamesf1982 的回复:]
typedef struct
{
int aInt;
double aDouble;
}testStruct,*ptestStruct;

我们一般这样定义一个结构
[/Quote]
zhongfei5006 2009-10-17
  • 打赏
  • 举报
回复
呵呵 鼓励从C开始
东大坡居士 2009-10-16
  • 打赏
  • 举报
回复
用GCC是好的啊
lbh2001 2009-10-16
  • 打赏
  • 举报
回复
其实支持C99标准的编译器也可以用这种随机声明后使用的机制,但C89不支持
[Quote=引用 11 楼 infidel 的回复:]
9楼回复的正确~晕的很C语言还有这样的要求……C++里面想咋样就咋样~ +_+ !!!
这年头~
[/Quote]
#include <stdio.h>
#include <stdlib.h>

struct testStruct
{
int aInt;
double aDouble;
};

typedef struct testStruct TestStruct;
typedef struct testStruct* pTestStruct;

int main(void)
{
pTestStruct structArray[100];
struct testStruct test;
test.aInt = 100;
test.aDouble = 10000.0;
printf("%d\n%f\n", test.aInt, test.aDouble);

structArray[0] = (pTestStruct)malloc(sizeof(TestStruct));
structArray[0]->aInt = 100000;
structArray[0]->aDouble = 9998.2121;

printf("%d\n%f\n", structArray[0]->aInt, structArray[0]->aDouble);
free(structArray[0]);

return 0;
}
asimay 2009-10-16
  • 打赏
  • 举报
回复
貌似头文件少了。。
stdlib.h..等
InfidelX 2009-10-16
  • 打赏
  • 举报
回复
9楼回复的正确~晕的很C语言还有这样的要求……C++里面想咋样就咋样~ +_+ !!!
这年头~
james_hw 2009-10-16
  • 打赏
  • 举报
回复
typedef struct
{
int aInt;
double aDouble;
}testStruct,*ptestStruct;

我们一般这样定义一个结构
echo_love 2009-10-16
  • 打赏
  • 举报
回复
#include "stdio.h"

struct testStruct
{
int aInt;
double aDouble;
};

typedef struct testStruct TestStruct;
typedef struct testStruct* pTestStruct;

main()
{
struct testStruct test;
struct testStruct* structArray[100];
test.aInt = 100;
test.aDouble = 10000.f;
printf("%d\n%f\n",test.aInt,test.aDouble);



structArray[0] = (pTestStruct)malloc(sizeof(TestStruct));
structArray[0]->aInt = 100000;
structArray[0]->aDouble = 9998.2121f;

printf("%d\n%f\n",structArray[0]->aInt,structArray[0]->aDouble);
free(structArray[0]);
}

//不能用typedef定义结构体指针 c语言所有的变量要在最前面声明
这样改就行了
zmmcoko 2009-10-16
  • 打赏
  • 举报
回复
这程序写的很奇怪,没什么意思啊!
1.首先结构体里面的第二个变量是double类型的,下面赋值给的是float类型
2.输出的时候也是按照float类型输出,让人没法理解
VS2005的代码 貌似不是那样写的,你那样的还是vc6.0的吧!
Johnny_Lx 2009-10-16
  • 打赏
  • 举报
回复
没问题
echo_love 2009-10-16
  • 打赏
  • 举报
回复
好像不可以用typedef定义了结构指针类型
wczzywc 2009-10-16
  • 打赏
  • 举报
回复
帮挺一下,我也想知道
InfidelX 2009-10-16
  • 打赏
  • 举报
回复
楼上建立的C文件还是C++文件?
C++文件没有什么问题的,但是C文件好像有问题的说。
wanjingwei 2009-10-16
  • 打赏
  • 举报
回复
vs2005上试了,可以运行
输出
100
10000.000000
100000
9998.211914
InfidelX 2009-10-16
  • 打赏
  • 举报
回复
1楼的,包含了malloc还是不对,编译不通过,output如下:
1>------ Build started: Project: CStruct, Configuration: Debug Win32 ------
1>Compiling...
1>test.c
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(19) : error C2275: 'pTestStruct' : illegal use of this type as an expression
1> f:\solutions - windowsprogramming\cstruct\cstruct\test.c(10) : see declaration of 'pTestStruct'
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(19) : error C2146: syntax error : missing ';' before identifier 'structArray'
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(19) : error C2065: 'structArray' : undeclared identifier
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(19) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(21) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(22) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(23) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(25) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(25) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(26) : error C2109: subscript requires array or pointer type
1>f:\solutions - windowsprogramming\cstruct\cstruct\test.c(26) : error C2198: 'free' : too few arguments for call
1>Build log was saved at "file://f:\Solutions - WindowsProgramming\CStruct\CStruct\Debug\BuildLog.htm"
1>CStruct - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
wanjingwei 2009-10-16
  • 打赏
  • 举报
回复

#include "stdio.h"
#include "malloc.h"
struct testStruct
{
int aInt;
double aDouble;
};

typedef struct testStruct TestStruct;
typedef struct testStruct* pTestStruct;

int main()
{
struct testStruct test;
test.aInt = 100;
test.aDouble = 10000.f;
printf("%d\n%f\n",test.aInt,test.aDouble);

pTestStruct structArray[100];

structArray[0] = (pTestStruct)malloc(sizeof(TestStruct));
structArray[0]->aInt = 100000;
structArray[0]->aDouble = 9998.2121f;

printf("%d\n%f\n",structArray[0]->aInt,structArray[0]->aDouble);
free(structArray[0]);
return 0;
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧