编写程序把一个数组中的所有内容前后颠倒(要求使用指针完成)
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
int *p_pre = arr, *p_post = arr + 6;
printf("*p_pre = %d,*p_post = %d\n", *p_pre, *p_post);
while (p_pre < p_post)
{
*p_pre += *p_post;
*p_post = *p_pre - *p_post;
*p_pre = *p_pre - *p_post;
p_pre++;
p_post--;
}
for (p_pre = arr; p_pre <= arr + 6; p_pre++)
{
printf("%d ", *p_pre);
}
printf("\n");
return 0;
}
编写程序从一个大数组中查找另一个小数组,把查找到的所有起点打印在屏幕上
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 3, 4, 6, 7, 3, 4, 5};
int arr1[] = {3, 4, 5};
int *p_cur = NULL, *p_arr1 = NULL;
for (p_cur = arr; p_cur <= arr + 11; p_cur++)
{
for (p_arr1 = arr1; p_arr1 <= arr1 + 2; p_arr1++)
{
if (*(p_arr1 - arr1 + p_cur) != *p_arr1)
{
break;
}
}
if (!(p_arr1 <= arr1 + 2))
{
printf("位置%d是一个查找结果\n", p_cur - arr);
}
}
return 0;
}
|