首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言结构体指针的初始化有关问题

2012-03-25 
C语言结构体指针的初始化问题#includestdio.hstruct student{int numint score}struct student stus

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>


[解决办法]
语句得写到函数中

C/C++ code
#include<stdio.h>struct student{ int num; int score;};struct student stu;struct student *p;int main(){   p=&stu;}
[解决办法]
定义可以写在函数外面
C/C++ code
struct student stu,*p=&stu;; 

热点排行
Bad Request.