结构体和函数指针
2015-12-13 13:56
204 查看
1、简单结构体
struct student{
char name[20]; //可以用scanf或者直接赋值
*如果用char *name 在用scanf时没有内存接收
long id;
int age;
float height;
};
结构体中只能声明变量不能赋初值。
struck student zhangsan;
struck student zhangsan = {"xiaowang",2000002,20,180.5};
结构体的访问用".":xiaowang.name
2、typedef
typedef struct student{
char name[20]; //不能用char *name 在用scanf时没有内存接收
long id;
int age;
float height;
}Student; // typedef给一个存在的类型取一个别名
Student zhangsan;
Student zhangsan = {"xiaowang",2000002,20,180.5};
如果不加typedef:
struct student{
char name[20]; //不能用char *name 在用scanf时没有内存接收
long id;
int age;
float height;
}Student;//Student 是一个变量了
3、结构体指针
Student *s;
如果*name是字符串 s->name = "xiaowang";
如果name[]是数组接收 strcpy(s->name,"xiaowang");
s->age = 23;
Student *s[5]; //每一块都存着结构体的地址
Student xw ={"xiaowang",2345,23,164.3};
s[0] =&xw; //结构体指针数组里面的每一个都存着地址,如果不给他内存地址,它的值就为空,不可直接赋值。
s[0]->age = 20;
4、结构体数组
Student array[5] ={};
strcpy(array[0].name,"xiaowang");
array[0].age = 23;
5、用函数指针实现回调(block)功能
#include <stdio.h>
int add(int a, int b, void (*pFunction)(int)){
//。。。。。
pFunction(a+b);
return a + b;
}
void funA(int a){
printf("a+%d\n", a);
}
void funB(int a){
printf("a-%d\n", a);
}
void funC(int a){
printf("a*%d\n", a);
}
int main(int argc, const char * argv[]) {
//一个函数指针只能指向一种类型的函数
void (*pFunction[3])(int);
pFunction[0] = funA;
pFunction[1] = funB;
pFunction[2] = funC;
for (int i = 0; i < 3; i++) {
pFunction[i](3);
}
return 0;
}
计算结构体内存空间
原理:如果结构体内部拥有多种数据类型,那么以占据内训字节数最高的类型对齐
typedef struct{
char *name;
int age;
}Persong;//16
char *占据8个字节,int占据4个字节
所以age变量自动向name对齐,整个占据16个字节
typedef struct{
char name;
int age;
}Person;//8
typedef struct{
char name[2];
int age;
}Person;//8
typedef struct{
char name[6];
int age;
}Person;//12
在定义结构体的时候,结构体里面的变量必须是能够明确确定内存空间的
struct student{
char name[20]; //可以用scanf或者直接赋值
*如果用char *name 在用scanf时没有内存接收
long id;
int age;
float height;
};
结构体中只能声明变量不能赋初值。
struck student zhangsan;
struck student zhangsan = {"xiaowang",2000002,20,180.5};
结构体的访问用".":xiaowang.name
2、typedef
typedef struct student{
char name[20]; //不能用char *name 在用scanf时没有内存接收
long id;
int age;
float height;
}Student; // typedef给一个存在的类型取一个别名
Student zhangsan;
Student zhangsan = {"xiaowang",2000002,20,180.5};
如果不加typedef:
struct student{
char name[20]; //不能用char *name 在用scanf时没有内存接收
long id;
int age;
float height;
}Student;//Student 是一个变量了
3、结构体指针
Student *s;
如果*name是字符串 s->name = "xiaowang";
如果name[]是数组接收 strcpy(s->name,"xiaowang");
s->age = 23;
Student *s[5]; //每一块都存着结构体的地址
Student xw ={"xiaowang",2345,23,164.3};
s[0] =&xw; //结构体指针数组里面的每一个都存着地址,如果不给他内存地址,它的值就为空,不可直接赋值。
s[0]->age = 20;
4、结构体数组
Student array[5] ={};
strcpy(array[0].name,"xiaowang");
array[0].age = 23;
5、用函数指针实现回调(block)功能
#include <stdio.h>
int add(int a, int b, void (*pFunction)(int)){
//。。。。。
pFunction(a+b);
return a + b;
}
void funA(int a){
printf("a+%d\n", a);
}
void funB(int a){
printf("a-%d\n", a);
}
void funC(int a){
printf("a*%d\n", a);
}
int main(int argc, const char * argv[]) {
//一个函数指针只能指向一种类型的函数
void (*pFunction[3])(int);
pFunction[0] = funA;
pFunction[1] = funB;
pFunction[2] = funC;
for (int i = 0; i < 3; i++) {
pFunction[i](3);
}
return 0;
}
计算结构体内存空间
原理:如果结构体内部拥有多种数据类型,那么以占据内训字节数最高的类型对齐
typedef struct{
char *name;
int age;
}Persong;//16
char *占据8个字节,int占据4个字节
所以age变量自动向name对齐,整个占据16个字节
typedef struct{
char name;
int age;
}Person;//8
typedef struct{
char name[2];
int age;
}Person;//8
typedef struct{
char name[6];
int age;
}Person;//12
在定义结构体的时候,结构体里面的变量必须是能够明确确定内存空间的
相关文章推荐
- C#生成缩略图代码
- android-Debugging Web Apps
- SFM-based positioning使用方法
- C#中的DllImport使用笔记
- 为什么梯度下降慢而用随机梯度下降
- 二叉树排序树中查找的路径
- struts2重要知识点总结(2)
- leetcode -- Permutation & Permutation II--重点
- POJ 3009 Curling 2.0【DFS】
- Objective-C语言——NSMutableString可变字符串
- [笔记] 大型网站技术架构——核心原理与案例分析 [六]
- Oracle字符乱码、数据越界访问典型Bug分析
- WiFi万能钥匙蹭网原理详细剖析
- 在投票系统方法的原则刷票(突破ip限制刷票PHP版)
- C++ Windows读写INI文件
- 杭电OJ4496 并查集 D-City
- 首记
- 模板元编程第九章练习(非答案)
- 利用js数组中的sort给数字排序
- JavaScript模块化开发之SeaJS