结构体的使用和动态内存的分配及释放
2013-07-17 09:46
190 查看
结构体
什么是结构体?结构体是用户根据实际需要自己定义的复合数据类型。结构体的出现是为了表示一些复杂的数据,而普通的数据类型无法满足要求。
结构体的定义:struct Student //struct Student为一个复合数据类型,结构体名字为Student,含有三个成员sno,name,age
{
int sno;
char name[20];
int age;
};//分号不能省
实例说明1:#include<stdio.h>
#include<string.h>
struct Student{
int sno;
char name[20];
int age;
};//分号不能省
int main()
{
struct Student st,*pst;
pst=&st;
/*第一种方式:结构体变量.成员*/
st.sno=99;
strcpy(st.name,"李四");//这里的strcpy(st.name,"李四")是string类型的赋值
st.age=21;
printf("%d,%s,%d\n",st.sno,st.name,st.age);
/*第二种方式:pst指向结构体变量中的sno成员,推荐使用*/
pst->sno=100;//pst->sno等价于(*pst).sno
strcpy(pst->name,"王五");
pst->age=30;
printf("%d,%s,%d\n",pst->sno,pst->name,pst->age);
return 0;
}
实例说明2(通过指针传参(在普通变量的数据类型大于4个字节时)可以节省内存和时间,还可以修改成员变量的值):#include<stdio.h>
#include<string.h>
struct Student{
int sno;
char name[20];
int age;
};
void input(struct Student *pst);//前置声明
void output(struct Student *pst);
int main()
{
struct Student st;//定义结构体变量st,同时为st分配内存(此时st占28个字节)
input(&st);
output(&st);
return 0;
}
void output(struct Student *pst)//完成输出
{
printf("%d %s %d\n",pst->sno,pst->name,pst->age);
}
void input(struct Student *pst)//完成输入
{
(*pst).sno=100;
strcpy(pst->name,"张三");
pst->age=21;
}
注意:
1.结构体在定义时并没有分配内存(它只是一个模型),而是在定义结构体变量时分配内存。
2.结构体变量(如上面的st)不能进行四则运算,但可以相互赋值。
动态内存的分配和释放
使用了malloc()函数的都可以称为动态分配内存。malloc()带一个整型参数
如:int *pArr=(int *)malloc(sizeof(int)*5);
说明:其中的malloc函数只能返回第一个字节的地址(无实际意义),所以要就行强制类型转换,这里加(int *);
动态内存的释放:free(pArr);
说明:把pArr所代表的动态分配的20个字节的内存释放(局部变量在函数内执行完就释放了),跨函数使用内存只能通过动态分配内存来实现。
实例说明(跨函数使用内存):
#include<stdio.h>
#include<malloc.h>
struct Student
{
int sno;
int age;
};
struct Student* CreateStudent(void);//void表示该方法不加形参,可不写
void ShowStudent(struct Student *);
int main()
{
struct Student *ps;
ps=CreateStudent();
ShowStudent(ps);//将ps赋给pst
free(ps);
return 0;
}
struct Student* CreateStudent(void)
{
struct Student *p=(struct Student*)malloc(sizeof(struct Student));//为结构体指针变量p动态分配内存
p->sno=1001;
p->age=22;
return p;//把p赋给ps,此时ps指向p所指内存地址
}
void ShowStudent(struct Student *pst)
{
printf("%d %d\n",pst->sno,pst->age);//通过pst指向结构体变量ps的成员sno、age进行输出成员值
}
什么是结构体?结构体是用户根据实际需要自己定义的复合数据类型。结构体的出现是为了表示一些复杂的数据,而普通的数据类型无法满足要求。
结构体的定义:struct Student //struct Student为一个复合数据类型,结构体名字为Student,含有三个成员sno,name,age
{
int sno;
char name[20];
int age;
};//分号不能省
实例说明1:#include<stdio.h>
#include<string.h>
struct Student{
int sno;
char name[20];
int age;
};//分号不能省
int main()
{
struct Student st,*pst;
pst=&st;
/*第一种方式:结构体变量.成员*/
st.sno=99;
strcpy(st.name,"李四");//这里的strcpy(st.name,"李四")是string类型的赋值
st.age=21;
printf("%d,%s,%d\n",st.sno,st.name,st.age);
/*第二种方式:pst指向结构体变量中的sno成员,推荐使用*/
pst->sno=100;//pst->sno等价于(*pst).sno
strcpy(pst->name,"王五");
pst->age=30;
printf("%d,%s,%d\n",pst->sno,pst->name,pst->age);
return 0;
}
实例说明2(通过指针传参(在普通变量的数据类型大于4个字节时)可以节省内存和时间,还可以修改成员变量的值):#include<stdio.h>
#include<string.h>
struct Student{
int sno;
char name[20];
int age;
};
void input(struct Student *pst);//前置声明
void output(struct Student *pst);
int main()
{
struct Student st;//定义结构体变量st,同时为st分配内存(此时st占28个字节)
input(&st);
output(&st);
return 0;
}
void output(struct Student *pst)//完成输出
{
printf("%d %s %d\n",pst->sno,pst->name,pst->age);
}
void input(struct Student *pst)//完成输入
{
(*pst).sno=100;
strcpy(pst->name,"张三");
pst->age=21;
}
注意:
1.结构体在定义时并没有分配内存(它只是一个模型),而是在定义结构体变量时分配内存。
2.结构体变量(如上面的st)不能进行四则运算,但可以相互赋值。
动态内存的分配和释放
使用了malloc()函数的都可以称为动态分配内存。malloc()带一个整型参数
如:int *pArr=(int *)malloc(sizeof(int)*5);
说明:其中的malloc函数只能返回第一个字节的地址(无实际意义),所以要就行强制类型转换,这里加(int *);
动态内存的释放:free(pArr);
说明:把pArr所代表的动态分配的20个字节的内存释放(局部变量在函数内执行完就释放了),跨函数使用内存只能通过动态分配内存来实现。
实例说明(跨函数使用内存):
#include<stdio.h>
#include<malloc.h>
struct Student
{
int sno;
int age;
};
struct Student* CreateStudent(void);//void表示该方法不加形参,可不写
void ShowStudent(struct Student *);
int main()
{
struct Student *ps;
ps=CreateStudent();
ShowStudent(ps);//将ps赋给pst
free(ps);
return 0;
}
struct Student* CreateStudent(void)
{
struct Student *p=(struct Student*)malloc(sizeof(struct Student));//为结构体指针变量p动态分配内存
p->sno=1001;
p->age=22;
return p;//把p赋给ps,此时ps指向p所指内存地址
}
void ShowStudent(struct Student *pst)
{
printf("%d %d\n",pst->sno,pst->age);//通过pst指向结构体变量ps的成员sno、age进行输出成员值
}
相关文章推荐
- 结构体的使用和动态内存的分配及释放
- 使用new和delete[]进行动态内存分配和释放
- 数据结构预备知识之指针,结构体和动态内存的分配与释放
- TCMalloc的使用与源码剖析之八---------TCMalloc内存分配与释放的管理之内存泄露检查
- 输出学生成绩。本题要求编写程序,根据输入学生的成绩,统计并输出学生的平均成绩、最高成绩和最低成绩。建议使用动态内存分配来实现。
- 函数:函数操作结构体通过按值传递以及按址传递,使用动态内存
- C语言实现一、二维数组动态内存分配与释放以及图像的线性插值放大与优化
- 关于C++中结构体及其指针成员的内存分配和释放
- C和C++中内存分配和释放使用介绍
- 结构体指针数组 内存分配 释放
- 自说动态内存分配与释放
- 【C】【笔记】《C和指针》第10章 结构和联合 第11章 动态内存分配 第12章 使用结构和指针 第13章 高级指针话题 第14章预处理器
- 使用CRT调试内存分配堆来找出未释放的内存空间
- C/C++中的二维数组动态内存分配与释放
- 警惕利用类的构造和析构函数来做资源分配释放时候,对临时变量的使用
- 使用malloc()和free()函数分配和释放内存
- C语言之_动态内存分配、结构体指针、链表
- C++指针参数 动态内存分配和释放
- 05-数据结构_预备知识-动态内存的分配和释放
- 使用指向指针的指针进行动态内存分配