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;
}


结构体变量的赋初值还可以用以下的方式进行:
#include<iostream>
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;
}
10.对结构体变量的声明,系统是不会为它分配内存空间的,系统只会为结构体变量分配内存空间。
11.下面一段代码:
(1)普通引用:
int a=10;
int &b=a;//b引用了a,b是a的别名。b的改变可以直接改变a的值。
(2)常引用:
const int &x=a;
cout<<x<<endl;
//x=112;编译是不能通过的。
12.下面一段代码:
#include<iostream>
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;这两种写法是等价的。
}
13.指针既可以改变所指向的值也可以改变自身的值。
14.下面一段代码:
#include<iostream>
using namespace std;
void main()
{
char str[100];
char *sp;
strcpy(str,"wangpeng ");
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值