读书人

新手关于strcut定义的疑问解决办法

发布时间: 2012-02-13 17:20:26 作者: rapoo

新手关于strcut定义的疑问
我在某个帖子看到:
struct student
{
long id;
char name[20];
char banji[10];
int score[10];
struct student * next;
};
但是我学c++是最后那里只需要写student* next;就好了,不用前面的struct。
请问这是因为一个是c++,另一个是c语言的原因吗?是因为两种语言定义方式略有不同还是说上面的定义才是规范的定义,或者还有其他什么原因之类的。
我想弄清楚这点,麻烦大家知道告诉我,谢谢

[解决办法]
在标准C中
因为一个struct要用到的时候,必须带这struct限定词
eg.
struct tagstudent
{
int studentID;
char studentName[256];
};

以后使用该定义的时候,你得
struct tagstudent students[5];
得带着个struct才行
所以大家为了方便,就这样写
typedef struct tagstudent
{
int studentID;
char studentName[256];
}student;


以后只要想定义这个结构
student students[5];
就ok了。

但是在C++的语法里,struct定义后就不需要在定义的时候带着struct了

所以typedef也就失去了它应有的作用

读书人网 >C++

热点推荐