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

构造体struct

2012-08-09 
结构体structstruct stu{}student问题是student在现在的位置和写在stu后面有什么区别。新手。谢谢。[解决办法

结构体struct
struct stu
{
}student
 

问题是student在现在的位置和写在stu后面有什么区别。
新手。谢谢。

[解决办法]
student写在}的后面的意思是,立即用这个名字开辟一个这个结构体的空间,可以马上储存数据了。

stu写在{的前面,意思就是以后“stu”就代表了这个结构体,直接用“stu xx”就可以开辟空间了,xx就是新空间的名字
[解决办法]
下面我写一个小例子

C/C++ code
struct 结构体名{    int 数值;}结构体;//顺便以这个名字开辟一个空间结构体.数值=0;//现在可以往这个空间里写数据了结构体名 结构体2;//开辟另一个空间结构体2.数值=2;//往这个空间存放数据
[解决办法]
楼上++
[解决办法]
struct point {
int x;
int y;
};
The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.

struct { ... } x, y, z;
is syntactically analogous to
int x, y, z;
[解决办法]
struct stu
{
}student


student是一个具体的内容,是属于stu这一类的一个结构名,。其实写在{前 或写在}后的作用是一样的,都是开辟一定量的内存空间,至于内存空间的大小,则由stu这个结构体名决定。

比如struct stu
{
int a;
double b;
}student 


struct stu student
{
int a;
double b;
}

的作用一样是开辟(int)+(double)这么大的内从空间,是以student为指针名称的内存。



struct stu teacher;
这条语句也一样是开辟以teacher为指针名称,空间大小一样的内存。


[解决办法]
在前面类似于用了typedef,在后面直接建立变量
[解决办法]
一个是变量,一个是类型。
[解决办法]
struct stu{
 ////////////
 ........ 
 ///////////
}student;

该结构体的名称是stu,相当于一个类型名;student是该结构体类型的一个变量。

另外:在C中你除了用上述方法定义结构体变量,还可以struct stu 变量名;
而在C++中,定义结构体变量时,struct这个关键字可以省略不写。

还有在C中定义结构体变量而不写struct这个关键字的方法:
typedef struct stu{
////////////
............
////////////
}STU;

这时STU是类型struct stu的替代,(注意STU是类型名)。这时定义变量时:STU 变量名。


[解决办法]
前面的用于在结构体内部

例如定义指向结构自己的指针




看 <<c和指针>> 里面有详细写
[解决办法]
student是变量名;
stu是结构体名。

热点排行