题目讲解
1.函数内部局部变量会在调用结束后销毁,指针会变成野指针,谁也不知道里面有啥
2.
3.
释放指针之后,就不要再操作了
4.求 最小公倍数和最大公约数
10%30所得为10,被除数较小时,自己就是余数
int main()
{
int m, n;
while (scanf("%d %d", &m, &n) == 2)
{
int i = m;
int j = n;
int r = 0;
while (r = i % j)
{
i = j;
j = r;
}
printf("%d", j + m * n / j);
//循环结束时j就是最大公约数
}
return 0;
}
柔性数组
结构中的最后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员。
实现方法:
不好说哪个好,但是道理是对的
不推荐第二种,1.malloc,realloc多,容易忘记free导致内存泄漏 2.malloc用的多,内存碎片化
typedef struct s
{
int n;
int arr[];
}rou;
int main()
{
rou* h = (rou*)malloc(sizeof(rou));
rou* h = (rou*)realloc(h, sizeof(rou) + 40);//给arr开辟40个字节
}
//另一种是
typedef struct s
{
int n;
int* arr;
}rou;
int main()
{
h->arr = (rou*)malloc(4);//给arr开辟4个字节
h->arr = (rou*)realloc(h->arr,40);//扩容40个字节
}