这是代码。
#include <stdio.h>
#define N 3
#define COPY(a, i) (a[(i)]) = (a[((i)+1)])
enum course {BTP300 = 1, OOP244, OOP344, OOP444, BTP400 = 8, BTP500};
typedef enum course Course;
void display(void* a, int n) {
int i;
unsigned char* c = (unsigned char*)a;
for (i = 0; i < n; i++)
printf("%d ", c[i]);
printf("\n");
}
void process(void *c, int n, int s) {
int i, j;
unsigned char* a = (unsigned char*)c;
for (i = 0; i < s * n; i++) {
unsigned char x = a[i];
for (j = 1; j < s - 1; j++, i++)
COPY(a, i);
a[++i] = x;
}
}
int main() {
Course array[2][N] = {BTP300, BTP400, BTP500, OOP244, OOP344, OOP444};
display(array[1], sizeof(Course)*N);
display(array[0], sizeof(Course)*N);
process(array[0], N, sizeof(Course));
process(array[1], N, sizeof(Course));
display(array[1], sizeof(Course)*N);
display(array[0], sizeof(Course)*N);
return 0;
}
输出是:
2 0 0 0 3 0 0 0 4 0 0 0
1 0 0 0 8 0 0 0 9 0 0 0
0 0 0 2 0 0 0 3 0 0 0 4
0 0 0 1 0 0 0 8 0 0 0 9
现在,当指针大小开始起作用时,它看起来像什么。我最初认为虽然创建了内存,但在数组中你只是跳过。所以我仍然会得到 234。但没有。我得到 1byte 字符。
0 2
1 0
2 0
3 0
这也会被打印出来。
这是怎么回事?