-
Notifications
You must be signed in to change notification settings - Fork 0
/
运用指针的方式给数组逆序
61 lines (47 loc) · 1002 Bytes
/
运用指针的方式给数组逆序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#define N 5
int main()
{
int i;
int temp;
int array[N] = {0,98,12,45,66};
int * ptr_array = array;
int * ptr_array2 = array + N - 1;
for(i = 0;i < N;i++)
{
printf("%d\t%p\n",*(array+i),array+i);
}
printf("\n\n");
while(ptr_array != ptr_array2)
{
temp = *ptr_array;
*ptr_array = *ptr_array2;
*ptr_array2 = temp;
ptr_array++;
ptr_array2--;
}
for(i = 0;i < N;i++)
{
printf("%d\t%p\n",*(array+i),array+i);
}
/*
printf("原来排序:");
for(i = 0;i < N;i++)
{
printf("%d\t%p\n",*ptr_array+i,ptr_array+i);
}
for(i = 0;i < N/2;i++)
{
temp = *(ptr_array+i);
*(ptr_array+i) = *(ptr_array+N-i-1);
*(ptr_array+N-i-1) = temp;
}
printf("逆序后的数:\n");
for(i = 0;i < N;i++)
{
printf("%d\t%p\n",*(ptr_array+i),ptr_array+i);
}
*/
return 0;
}