我使用Visual C++ 6.0练习指针人员分配结构变量,下面是我的代码:麻烦与存储空间和初始化
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char first_name[50];
char middle_name[50];
char last_name[50];
int height;
int weight;
}people;
int main()
{
//declare a people structure pointer
people *ppl_ptr;
//allocate memory space for people structure
ppl_ptr = malloc(sizeof(people));
//check if you run out of the heap
if(ppl_ptr==NULL)
{
printf("Out of memory!\n");
return 0;
}
//initialize the unname people varible
(*ppl_ptr)={"bla","bla","bla",192,58};
return 0;
}
,但我得到了一个语法错误: C:\C Practice\ch14.c(30) : error C2059: syntax error : '{' Error executing cl.exe.
如果我更换 (*ppl_ptr)={"bla","bla","bla",192,58};
的东西,如 (*ppl_ptr).first_name="bla";
我会得到一个不同的错误:
ch14.c
C:\C Practice\ch14.c(30) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.
这个错误味精并没有真正意义的我...这是否曾想到你的人?请让我知道它为什么给我这个错误消息。提前致谢。
切向注意:Visual Studio 6已经有14年了,所以在支持C++的新特性方面已经严重过时了。我建议你升级到一些现代化的东西(Visual Studio 2010 Express是免费的,并且14年以上是最新的!)。 – 2012-03-31 16:00:18
@OliverCharlesworth这是一个C问题(VC++ 6有一个C编译器和一个C++编译器)。我们可以告诉他他正在使用C,因为他没有得到编译器错误,因为没有投出malloc!但升级编译器当然是一个好主意,特别是考虑到最近版本的VS Express是免费的。 – 2014-12-08 04:14:40