char *cp; const char *ccp; ccp = cp;
联合与结构最大的区别是后者每个成员依次存储,而联合每个成员都从偏移地址零开始存储,每个成员的位置重叠在一起,某个时刻只有一个成员真正被使用。
union bits32_tag { int whole; struct {char c0, c1, c2, c3;} byte; }value;
int array[100], array2[100]; void fun1(int *ptr) { ptr[1] = 3; *ptr = 3; ptr = array2; } void fun1(int arr[]) { arr[1] = 3; *arr = 3; arr = array2; } int main(int argc, char* argv[]) { array[1] = 3; *array = 3; array = array2;// error,这里会报错。 return 0; }
char vegetables[][10] = { "carrot", "celery", "corn", }; char *vegetables[] = { "carrot", "celery", "corn", };
int *weights[] = { {1,2,3,4,5}, {6, 7}, {8,9,10} };
int row_1[] = {1,2,3,4,5}; int row_2[] = {6, 7}; int row_3[] = {8,9,10}; int *weights[] = { row_1, row_2, row_3 };
char ga[] = "abcdef"; void my_array_func(char ca[10]) { printf("addr of array param = %#x \n", &ca); printf("addr of array param = %#x \n", ca); printf("addr of array param[0] = %#x \n", &(ca[0])); printf("addr of array param[1] = %#x \n", &(ca[1])); } void my_pointer_func(char *pa) { printf("addr of ptr param = %#x \n", &pa); printf("addr of ptr param = %#x \n", pa); printf("addr of ptr param[0] = %#x \n", &(pa[0])); printf("addr of ptr param[1] = %#x \n", &(pa[1])); } int main(int argc, char* argv[]) { printf("addr of ga = %#x \n", &ga); printf("addr of ga = %#x \n", ga); printf("addr of ga param[0] = %#x \n", &(ga[0])); printf("addr of ga param[1] = %#x \n", &(ga[1])); printf("****************************** \n"); my_array_func(ga); printf("****************************** \n"); my_pointer_func(ga); return 0; }
addr of ga = 0x425168 addr of ga = 0x425168 addr of ga param[0] = 0x425168 addr of ga param[1] = 0x425169 ****************************** addr of array param = 0x12fef8 addr of array param = 0x425168 addr of array param[0] = 0x425168 addr of array param[1] = 0x425169 ****************************** addr of ptr param = 0x12fef8 addr of ptr param = 0x425168 addr of ptr param[0] = 0x425168 addr of ptr param[1] = 0x425169 Press any key to continue