string函数族
1.strcpy
#include <string.h>
char *strcpy(char *dest, const char *src);
功能:实现字符串的复制
参数:dest:目标字符串首地址
src:源字符串首地址
返回值:目标字符串首地址
#include <stdio.h>
#include <string.h>
int main()
{
char str[32];
char ch[32] = "world";
strcpy(str, ch);
printf("%s\n", str);
return 0;
}
char *strncpy(char *dest, const char *src, size_t n);
功能:实现字符串复制
参数:dest:目标字符串首地址
src:源字符串首地址
n:字符的个数
返回值:目标字符串首地址
#include <stdio.h>
#include <string.h>
int main()
{
char str[32] = "hello";
char ch[32] = "world";
strncpy(str, ch, 2);
printf("%s\n", str);
return 0;
}
2.strlen
#include <string.h>
size_t strlen(const char *s);
功能:计算字符串的实际长度
参数:s:字符串的首地址
返回值:实际长度
3.strcat
#include <string.h>
char *strcat(char *dest, const char *src);
功能:用于字符串拼接
参数:dest:目标字符串首地址
src:源字符串首地址
返回值:目标字符串首地址
#include <stdio.h>
#include <string.h>
int main()
{
char str[32] = "hello";
char ch[32] = "world";
strcat(str, ch);
printf("%s\n", str);
return 0;
}
char *strncat(char *dest, const char *src, size_t n); 拼接 str 的前n个字符
功能:实现字符串拼接
参数:dest:目标字符串首地址
src:源字符串首地址
n:字符的个数
返回值:目标字符串首地址
4.strcmp
#include <string.h>
int strcmp(const char *s1, const char *s2);
功能:用于字符串比较
参数:s1、s2 字符串的首地址
返回值:
从字符串首个字符开始比较字符ASCII的大小,如果相等继续向后比较
1 s1 > s2
0 s1 == s2
-1 s1 < s2
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "hello";
char s2[] = "worldworld";
char *s3 = "ikun";
char *s4 = "nihao";
char *s5 = "helloworld";
char *s6 = "helloworld";
// int ret = strcmp(s1, s2); // -1
// int ret = strcmp(s1, s3); // -1
// int ret = strcmp(s2, s3); // 1
// int ret = strcmp(s2, s4); // 1
int ret = strcmp(s5, s6); // 0
printf("%d\n", ret);
return 0;
}
int strncmp(const char *s1, const char *s2, size_t n); 比较两个字符串前n个字符的大小
递归函数
1.定义:自己调用自己
2.执行过程分为两个阶段
a.递推阶段:从原问题出发,按递推公式从未知到已知最终达成递归的终止条件
b.回归阶段:按递推的终止条件求出结果,逆向逐步带入递归公式,回到原问题求解
3.递归的两个必要条件
a.存在限制条件,当满足这个限制条件的时候,递推便不再继续
b.每次递推之后会越来越接近这个限制条件
结构体
1.定义:
用户自定义的数据类型,在结构体中可以包含若干个不同数据类型的成员变量(也可以相同),使这些数据项组合起来反应某一个信息。
2.格式:
struct 结构体名
{
数据类型 成员变量1;
数据类型 成员变量2;
数据类型 成员变量3;
};
3.结构体变量
3.1.概念:
通过结构体类型定义的变量
3.2.格式:
struct 结构体名 变量名;
1)先定义结构体,在定义结构体变量
struct 结构体名
{
成员变量;
};
struct 结构体名 变量名;
#include <stdio.h>
struct student
{
int id;
int age;
char sex;
float score;
};
int main(int argc, char const *argv[])
{
struct student stu;
return 0;
}
2)定义结构体的同时,定义结构体变量
struct 结构体名
{
成员变量;
} 变量名;
#include <stdio.h>
struct student
{
int id;
int age;
char sex;
float score;
} stu; // 这个是全局的
int main(int argc, char const *argv[])
{
return 0;
}
3)缺省结构体名定义结构体变量
struct
{
成员变量;
} 变量名;
#include <stdio.h>
struct
{
int id;
int age;
char sex;
float score;
} stu;
int main(int argc, char const *argv[])
{
// struct student stu;
return 0;
}
4.赋值
4.1.定义变量时直接用大括号赋值
#include <stdio.h>
struct student
{
int id;
int age;
char sex;
float score;
};
int main(int argc, char const *argv[])
{
struct student stu = {1, 18, ' ', 60};
return 0;
}
4.2.定义变量时未初始化,然后对变量单独赋值
struct student
{
int id;
int age;
char sex;
float score;
};
int main(int argc, char const *argv[])
{
struct student stu;
stu.id = 3;
stu.age = 23;
stu.sex = ' ';
return 0;
}
4.3.点等法赋值
#include <stdio.h>
struct student
{
int id;
int age;
char sex;
float score;
};
int main(int argc, char const *argv[])
{
struct student stu = {
.id = 3,
.age = 19,
.score = 72,
};
return 0;
}
5.访问
通过 . 访问:结构体变量名.成员变量
scanf(“%d %d %f”, &stu.id, &stu.age, &stu.score);
printf(“%d %d %.2f\n”, stu.id, stu.age, stu.score);
6.重定义 typedef
typedef int size_t;
typedef int * int_p;
int a == size_t a;
int *p = &a; == int_p p = &a;
6.1.定义结构体的同时进行重定义
typedef struct student
{
int id;
int age;
char sex;
float score;
} STU;
STU stu; // == struct student stu
6.2.先定义结构体,然后重定义
struct student
{
int id;
int age;
char sex;
float score;
};
typedef struct student STU;
STU stu;
结构体数组
1.概念:
结构体类型相同的变量组成的数组
格式
2.1.定义结构体的同时定义结构体数组
struct student
{
int id;
int age;
int score;
} stu[3];
2.2.先定义结构体,然后定义结构体数组
struct student
{
int id;
int age;
int score;
};
struct student stu[3];
3.初始化:
3.1.定义结构体数组同时赋值
struct student
{
int id;
int age;
int score;
} stu[3] = {
{1, 20, 36},
{2, 19, 27},
{3, 23, 45},
};
3.2.先定义结构体数组,在对结构体数组的每一个元素分别赋值
struct student
{
int id;
int age;
int score;
} stu[3];
stu[0].id =1;
stu[0].age = 20;
stu[1].id = 2;
4.结构体数组大小
sizeof(结构体数组名);
元素个数 * 结构体类型大小
5.结构体数组输入输出(for循环)
struct student
{
int id;
int age;
int score;
} stu[3];
int main(int argc, char const *argv[])
{
for (int i = 0; i < 3; i++)
{
scanf("%d %d %d", &stu[i].id, &stu[i].age, &stu[i].score);
}
for (int j = 0; j < 3; j++)
{
printf("%d %d %d\n", stu[j].id, stu[j].age, stu[j].score);
}
}
结构体指针
1.概念:
指向结构体的指针
2.格式:
struct 结构体名 *结构体指针名;
#include <stdio.h>
struct student
{
int id;
int age;
float score;
}stu1, stu2;
struct work
{
int id;
int age;
float score;
}w1, w2;
int main(int argc, char const *argv[])
{
struct student *p = &stu1;
struct student *p2 = &w1; // 错误,结构体类型不匹配
return 0;
}
3.赋值:
格式:结构体指针变量名 -> 成员变量名
#include <stdio.h>
struct student
{
int id;
int age;
float score;
}stu1, stu2;
struct work
{
int id;
int age;
float score;
}w1, w2;
int main(int argc, char const *argv[])
{
struct student *p = &stu1;
struct work *p2 = &w1;
p -> id = 2;
p -> age = 20;
p -> score = 70;
printf("%d %d %f\n", p -> id, p -> age, p -> score);
return 0;
}
(*p).id = 2
4.结构体指针的大小
本质是指针:4字节
总结:
- 不能把结构体类型变量作为整体引用,只能对结构体类型变量中的各个成员变量分别引用
- 如果成员变量本身属于另一种结构体类型,用若干个成员运算符一级级找到最低级的成员变量