C语言结构体指针的初始化问题
时间:2011-12-13
来源:互联网
#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>
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>
作者: wxliu619 发布时间: 2011-12-13
语句得写到函数中
C/C++ code
C/C++ code
#include<stdio.h> struct student { int num; int score; }; struct student stu; struct student *p; int main() { p=&stu; }
作者: akirya 发布时间: 2011-12-13
定义可以写在函数外面
C/C++ code
C/C++ code
struct student stu,*p=&stu;;
作者: dongjiawei316 发布时间: 2011-12-13
p=&stu;如果是像这样的语句应该写在函数体里面,不能写在函数体外面,
struct student stu,*p=&stu;是初始化的时候就给指针赋除值,这样是可以的。
struct student stu,*p=&stu;是初始化的时候就给指针赋除值,这样是可以的。
作者: liru125504 发布时间: 2011-12-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28