C语言结构体指针的初始化问题
#include<stdio.h>
struct student
{
int num;
int score;
};
struct student stu;
struct student *p;
p=&stu;
int main()
{
return 0;
}
编译后:
--------------------Configuration: 123 - Win32 Debug--------------------
Compiling...
123.cpp
c:\documents and settings\administrator\123.cpp(8) : error C2501: 'p' : missing storage-class or type specifiers
c:\documents and settings\administrator\123.cpp(8) : error C2040: 'p' : 'int' differs in levels of indirection from 'struct student *'
c:\documents and settings\administrator\123.cpp(8) : error C2440: 'initializing' : cannot convert from 'struct student *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
123.obj - 3 error(s), 0 warning(s)
但是以下代码在编译时不会报错:
struct student
{
int num;
int score;
};
struct student stu,*p=&stu;;
int main()
{
return 0;
}
--------------------Configuration: 123 - Win32 Debug--------------------
Compiling...
123.cpp
123.obj - 0 error(s), 0 warning(s)
请高手指点迷津,谢谢!#include<stdio.h>
[解决办法]
语句得写到函数中
#include<stdio.h>struct student{ int num; int score;};struct student stu;struct student *p;int main(){ p=&stu;}
[解决办法]
定义可以写在函数外面
struct student stu,*p=&stu;;