1.qsort函数的实现:
void swap(char* buf1, char* buf2,size_t width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
void sort(void* base, size_t sz, size_t width, int (*cmp)(const void* e1,const void* e2))
{
int i = 0;
for (i = 0; i < sz - 1; i++)
{
int j = 0;
for (j = 0; j < sz - i - 1; j++)
{
//if (arr[j] > arr[j + 1])
if(cmp((char*)base+j*width,(char*)base+(j+1)*width)>0)
{
swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
}
}
}
}
2.sizeof和strlen的对比:
sizeof 只关注占⽤内存空间的⼤⼩,不在乎内存中存放什么数据。
strlen 是C语⾔库函数,功能是求字符串⻓度(\0之前)。
注:sizeof的操作数如果是一个表达式,那这个表达式不参与计算
int main()
{
short s = 10;
int i = 2;
int n = sizeof(s = i + 4);
printf("%d\n", s);//10
printf("%d\n", n);//2
return 0;
}
3.练习:数组和指针
1.sizeof:
int main()
{
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a));//16
printf("%d\n", sizeof(a + 0));//首元素地址-4/8
printf("%d\n", sizeof(*a));//4
printf("%d\n", sizeof(a + 1));//地址4/8
printf("%d\n", sizeof(a[1]));//4
printf("%d\n", sizeof(&a));//地址4/8
printf("%d\n", sizeof(*&a));//16
printf("%d\n", sizeof(&a + 1));//地址4/8
printf("%d\n", sizeof(&a[0]));//地址4/8
printf("%d\n", sizeof(&a[0] + 1));//地址4/8
char arr[] = {'a','b','c','d','e','f'};
printf("%d\n", sizeof(arr));//6
printf("%d\n", sizeof(arr+0));//地址4/8
printf("%d\n", sizeof(*arr));//1
printf("%d\n", sizeof(arr[1]));//1
printf("%d\n", sizeof(&arr));//地址4/8
printf("%d\n", sizeof(&arr+1));//地址4/8
printf("%d\n", sizeof(&arr[0]+1));//地址4/8
char arr[] = "abcdef";//a,b,c,d,e,f,/0
printf("%d\n", sizeof(arr));//7
printf("%d\n", sizeof(arr + 0));//首元素地址4/8
printf("%d\n", sizeof(*arr));//1
printf("%d\n", sizeof(arr[1]));//1
printf("%d\n", sizeof(&arr));//整个数组地址4/8
printf("%d\n", sizeof(&arr + 1));//跳过整个数组的地址4/8
printf("%d\n", sizeof(&arr[0] + 1));//第二个元素地址4/8
char *p = "abcdef";
printf("%d\n", sizeof(p));//p是字符串初始地址,a的地址---4/8
printf("%d\n", sizeof(p + 1));//b的地址4/8
printf("%d\n", sizeof(*p));//1
printf("%d\n", sizeof(p[0]));//1
printf("%d\n", sizeof(&p));//p的地址---4/8
printf("%d\n", sizeof(&p + 1));//地址---4/8
printf("%d\n", sizeof(&p[0] + 1));//b的地址---4/8
return 0;
}
2.strlen:
int main()
{
char arr[] = { 'a','b','c','d','e','f' };
printf("%d\n", strlen(arr));//随机值
printf("%d\n", strlen(arr + 0));//随机值
printf("%d\n", strlen(*arr));//传a---即97---非法访问,错误代码
printf("%d\n", strlen(arr[1]));//同理,错误代码
printf("%d\n", strlen(&arr));//自动由char(*)[6]转变为char*,同样获得首元素地址,随机值
printf("%d\n", strlen(&arr + 1));//随机值且与【上面随机值】差6
printf("%d\n", strlen(&arr[0] + 1));//随机值且与【上面随机值】差1
char arr[] = "abcdef";//a,b,c,d,e,f,/0
printf("%d\n", strlen(arr));//6
printf("%d\n", strlen(arr + 0));//6
printf("%d\n", strlen(*arr));//传a---即97---非法访问,错误代码
printf("%d\n", strlen(arr[1]));//同理,错误代码
printf("%d\n", strlen(&arr));//自动由char(*)[6]转变为char*,同样获得首元素地址,6
printf("%d\n", strlen(&arr + 1));//随机值
printf("%d\n", strlen(&arr[0] + 1));//5
char* p = "abcdef";
printf("%d\n", strlen(p));//6
printf("%d\n", strlen(p + 1));//5
printf("%d\n", strlen(*p));//错误
printf("%d\n", strlen(p[0]));//错误
printf("%d\n", strlen(&p));//随机值---p的地址开始查找
printf("%d\n", strlen(&p + 1));//随机值
printf("%d\n", strlen(&p[0] + 1));//5
return 0;
}