C语言学习内容总结2017/11/21(结构体)
1.结构体:
下面一段代码:
#include<iostream>using namespace std;
/*void main()
{
int num;
char name[20];
char sex[3] ;
int age;
float score;
};*/
//直接在函数里面定义
//结构体就是在设计新的类型。
//结构体的写法如下:
struct student //设计学生(结构体)的属性。
{
int id; //注意结构体在定义(设计)的时候不能赋初值。
char name[20];//注意结构体在定义(设计)的时候不能赋初值。
char sex[3]; //注意结构体在定义(设计)的时候不能赋初值。
int age; //注意结构体在定义(设计)的时候不能赋初值。
float score; //注意结构体在定义(设计)的时候不能赋初值。
};
//对结构体变量的赋初值:
int main()
{
student st;
st.id = 1;
st.name[20] = "wangpeng";
st.sex[3] = "男";
st.age = 23;
st.score = 100;
//char ar[10] = "男";
return 0;
}
/*那么为什么不能在定义结构体的时候对结构体里面的变量进行赋初值呢?这是因为:结构体相当于一张空白的图纸,在结构体里面只能定义结构体的数据属性,而不能对结构体里面的数据进行操作。*/
2.对于下面一段代码:
void main()
{
int a; //在内存中划分4byte的内存空间。但这段内存为随机值。
}
3.字符串常量属于数据区。
结构体中内存的对齐格式默认的是8byte。
利用#pragma pack(2) //括号里面可以是:1、2、4、8、16.可以干预struct内存的对齐方式。
4.在结构体里面可以嵌套结构体。
5.下面一段代码:
struct student //设计学生(结构体)的属性。
{
int id;
//注意结构体在定义(设计)的时候不能赋初值。
char name[20];//注意结构体在定义(设计)的时候不能赋初值。
char sex[3];
//注意结构体在定义(设计)的时候不能赋初值。
int age;
//注意结构体在定义(设计)的时候不能赋初值。
float score;
//注意结构体在定义(设计)的时候不能赋初值。
} stud;
//和student stud;是等价的。
6.typedef为类型重命名关键字。
下面一段代码:
#include<iostream>
using namespace std;
int main()
{
typedef unsigned int u_int, *point;
u_int a;
//变量a的类型为无符号整型。
point b = &a;
//变量b的数据类型为无符号整型变量的地址。
return 0;
}
7. 下面一段代码:
#include<iostream>
using namespace std;
typedef struct
{
int id;
//注意结构体在定义(设计)的时候不能赋初值。
char name[20];
//注意结构体在定义(设计)的时候不能赋初值。
char sex[3];
//注意结构体在定义(设计)的时候不能赋初值。
int age;
//注意结构体在定义(设计)的时候不能赋初值。
float score;
//注意结构体在定义(设计)的时候不能赋初值。
}stud, x, y;
int main()
{
stud n;
x m;
y l;
return 0;
}
8.下面一段代码 :
typedef struct
{
int id;
//注意结构体在定义(设计)的时候不能赋初值。
char name[20];
//注意结构体在定义(设计)的时候不能赋初值。
char sex[3];
//注意结构体在定义(设计)的时候不能赋初值。
int age;
//注意结构体在定义(设计)的时候不能赋初值。
float score;
//注意结构体在定义(设计)的时候不能赋初值。
}student;
和
struct student
{
int id;
//注意结构体在定义(设计)的时候不能赋初值。
char name[20];
//注意结构体在定义(设计)的时候不能赋初值。
char sex[3];
//注意结构体在定义(设计)的时候不能赋初值。
int age;
//注意结构体在定义(设计)的时候不能赋初值。
float score;
//注意结构体在定义(设计)的时候不能赋初值。
};
student student ;//是可以编译通过的。在定义结构体时,将结构体变量名与结构体名称定义成一样的可以防止监视。
而int int ;是不能编译通过的。
9.结构体变量的赋初值;
#include<iostream>
using namespace std;
struct student
{
int id;
//注意结构体在定义(设计)的时候不能赋初值。
char name[20];
//注意结构体在定义(设计)的时候不能赋初值。
char sex[3];
//注意结构体在定义(设计)的时候不能赋初值。
int age;
//注意结构体在定义(设计)的时候不能赋初值。
float score;
//注意结构体在定义(设计)的时候不能赋初值。
};
int main()
{
student st1={1001,"wangpeng","男",22,100};//如果这样赋初值的话,则必须严格按照结构体定义时的变量顺序,中间不能有跳隔。否则编译不通过。
student st2={1001};//这样的部分元素赋初值也是可以的,未赋值的结构体元素则会被自动赋值为0。
return 0;
}
using namespace std;
struct student
{
int id;
char name[20];
char sex[3];
int age;
float score;
};
int main()
{
student st1;
cin >> st1.id;
cin >> st1.name;
cin >> st1.sex;
cin >> st1.age;
cin >> st1.score;
return 0;
}
using namespace std;
struct student
{
int id;
char name[20];
char sex[3];
int age;
float score;
};
void main()
{
student s1 = {0};
student *sp = &s1;
(*sp).id = 100;//注意这块是要加括号的,因为点的优先级高于*。
//sp->id=100;这两种写法是等价的。
}
using namespace std;
void main()
{
char str[100];
char *sp;
strcpy(str,"wangpeng ");
}