C语言struct的问题
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 ==========
兄弟们帮我看看什么问题……
源代码如下:
#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 ==========
...全文
相关推荐
C语言结构体(struct)常见使用方法(细节问题)
C语言结构体(struct)常见使用方法(细节问题)C语言结构体(struct)是一种复杂数据类型,通过结构体,可以将多个变量封装到一个单元中,使得代码更加简洁和易于维护。
解析C语言中结构体struct的对齐问题
"本文主要解析了C语言中结构体struct的内存对齐问题,包括数据类型的默认对齐值、指定对齐值、有效对齐值的概念,并介绍了结构体成员的排放规则以及填充规则,帮助理解如何计算和处理结构体内部的字节对齐
struct和typedef struct区别
最后,我们探讨了一个和struct相关的问题,如果在C程序中我们写:```ctypedef struct { int num; int age;} aaa, bbb, ccc;```这实际上相当于:``
详解C语言Struct(结构体)系列之一
C语言中的结构体(Struct)是一种用户定义的数据类型,允许将不同类型的数据项聚集在一起构成一个单一类型。
c语言考研复试 面试常见问题集合
这些问题都是C语言开发中的热点问题,了解这些问题和答案对C语言开发者的职业发展和技术提高非常重要。1. 声明和初始化在C语言中,声明和初始化是非常重要的概念。


