无规矩不成方圆,无五音难正六律。做任何事情都要有规矩,懂规矩,守规矩。身为一名合格的程序员,那我们一定要知道“差之毫厘,谬以千里”的道理。为了在开发中做到,提高代码的可读性和可维护性,同时避免函数得重写,提高开发效率,我们需要掌握如下技巧:
宏
它是一种预处理器指令,在预编译阶段将宏名替换为后面的替换体 。
宏的定义
由三部分组成 #define WIDTH 960
预处理指令 宏名 替换体(多行可用 \ 延续)
#include <stdio.h>
#include <stdlib.h>
#define _width 1024 //宏命名规则同变量名
int main(void){
printf("width: %d\n", _width);
system("pause");
return 0;
}
宏定义的使用
- 不带参数的宏
- 在宏中使用参数 (#define FIRST(x) x+x)
#include <stdio.h>
#include <stdlib.h>
// 不带参数的宏
#define _width 1024
// 带参数的宏
#define SQUARE(x) x*x
#define MAX(x,y) x>y?x:y
int main(void){
printf("width: %d\n", _width);
// 宏展开相当于 printf("width: %d\n", 1024);
int i = 10;
int j = SQUARE(i); // 宏展开 j = i*i;
printf("j: %d\n", j);
printf("MAX(i,j):%d\n", MAX(i, j));
//宏展开 printf("MAX(i,j):%d\n", i>j?i:j);
int z = SQUARE(2+3); // 2+3*2+3 = 11
printf("z: %d\n", z);
system("pause");
return 0;
}
结构体
为什么要使用“结构”(结构体)?
当需要表示一些复杂信息时,使用单纯的数据类型很不方便。
比如:学生信息(学号,姓名,班级,电话,年龄)
什么是“结构”?
结构,就是程序员自定义的一种“数据类型”
是使用多个基本数据类型、或者其他结构,组合而成的一种新的“数据类型”。
结构的定义
struct 结构名 {
成员类型 成员名;
成员类型 成员名;
};
//实例:
struct student {
char name[16];
int age;
char tel[12];
char class_num[12];
};
/******特别注意: ***********
1)要以 struct 开头
2)最后要使用分号
3)各成员之间用分号隔开
*****************************/
/********心得体会: ***********
对于结构体给我感受比较深的就是,
它和我们平常写的变量相比,
是一个超级变量
*****************************/
结构的初始化
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//结构,就是程序员自定义的一种“数据类型”
struct student {
char name[16];
int age;
char tel[12];
};
//结构体包含结构体的情况
struct _class{
struct student rock;
struct student martin;
struct student zsf;
};
int main(void){
//结构体的初始化
//方式一 定义的时候初始化所有的属性
struct student rock = {"Rock", 38, "******"};
printf("rock 的姓名: %s 年龄: %d 电话: %s\n", rock.name, rock.age, rock.tel);
//方式二 定义的时候我们可以指定初始化的属性 VS/VC 不支持,但 gcc 是 支持的
struct student s1 ={.name="张三丰",.age = 100};
//方式三 单独初始化每一个属性
struct student s2;
strcpy(s2.name, "杨过");
s2.age = 40;
s2.tel[0]='\0';
printf("杨过的姓名: %s 年龄: %d 电话: %s\n", s2.name, s2.age, s2.tel);
//结构体中含有结构体
struct _class c1={
{"Rock", 38, "******"},
{"Martin", 38, "*******"},
{"张三丰",100,"******"}
};
printf("c1 班 martin 同学的姓名: %s 年龄: %d 电话: %s\n", c1.martin.name, c1.martin.age, c1.martin.tel);
system("pause");
return 0;
}
结构的使用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char name[16];
int age;
};
int main(void){
struct student s1, s2;
printf("请输入第一个学生的姓名:\n");
scanf_s("%s",s1.name, sizeof(s1.name));
printf("请输入第一个学生的年龄:\n");
scanf("%d", &s1.age);
printf("第一个学生的姓名: %s, 年龄: %d\n", s1.name, s1.age);
//结构体的小秘密,结构体变量之间可以直接赋值
//s2 = s1;
memcpy(&s2, &s1, sizeof(struct student));
printf("第二个学生的姓名: %s, 年龄: %d\n", s2.name, s2.age);
char c1[16]={"martin"}, c2[16];
//c2 = c1; //数组不能直接赋值
system("pause");
return 0;
}
结构数组
//struct 结构名 变量名[数组大小]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char name[16];
int age;
};
int main(void){
struct student s[2];
printf("请输入第一个学生的姓名:\n");
scanf_s("%s",s[0].name, sizeof(s[0].name));
printf("请输入第一个学生的年龄:\n");
scanf("%d", &s[0].age);
printf("第一个学生的姓名: %s, 年龄: %d\n", s[0].name, s[0].age);
//结构体的小秘密,结构体变量之间可以直接赋值
//s[1] = s[0];
memcpy(&s[1],&s[0], sizeof(struct student));
printf("第二个学生的姓名: %s, 年龄: %d\n", s[1].name, s[1].age);
system("pause");
return 0;
}
指向结构体的指针
//使用结构体变量址指针访问结构体成员要使用 -> 符号
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _friend{
char name[32];
char sex; // m - 男 f - 女
int age;
};
int main(void){
struct _friend girl = {"小龙女", 'f', 18};
struct _friend *my_girl = &girl;
printf("小龙女的名字:%s, 性别:%s 年龄:%d\n", girl.name, girl.sex=='m'?"男":"女", girl.age);
//指针访问结构体变量的成员,有两种方式
//方式 1. 直接解引
printf("小龙女的名字:%s, 性别:%s 年龄:%d\n", (*my_girl).name, (*my_girl).sex=='m'?"男":"女", (*my_girl).age);
//方式 2. 直接使用指针访问 ->
printf("小龙女的名字:%s, 性别:%s 年龄:%d\n", my_girl->name, my_girl->sex=='m'?"男":"女", my_girl->age);
system("pause");
return 0;
}
//实际开发中我们常用 -> 符通过指针去访问结构体指针变量的成员
使用结构体传递值
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct programer{
char name[32];
int age;
int salary;
};
//形参是结构体变量,值传递
struct programer add_salary(struct programer p, int num){
p.salary += num;
return p;
}
//形参使用结构体指针
void add_salary1(struct programer *p, int num){
if(!p) return ;
p->salary += num;
}
//形参使用引用
void add_salary2(struct programer &p, int num){
p.salary += num;
}
//形参是结构体变量,值传递,返回引用
struct programer& add_salary3(struct programer p, int num){
p.salary += num;
return p;
}
int main(void){
struct programer xiaoniu;
strcpy(xiaoniu.name, "小牛");
xiaoniu.age = 28;
xiaoniu.salary = 20000;
//结构体变量做为参数传值是值传递,和 int 等基本类型一样
//xiaoniu = add_salary(xiaoniu, 5000);
//指针传值
//add_salary1(&xiaoniu, 5000);
//引用传值
//add_salary2(xiaoniu, 10000);
//返回引用
xiaoniu = add_salary3(xiaoniu, 20000);
printf("姓名: %s, 年龄: %d, 薪水: %d\n", xiaoniu.name, xiaoniu.age, xiaoniu.salary);
system("pause");
return 0;
}
注意:
一般不建议把结构体直接作为函数参数。
因为结构体的 size 比较大,直接传递,消耗性能!
解决方案:(使用指针和引用,优先使用引用)