嵌入式软件工程师&&太极拳
全部博文(548)
分类:
2011-02-20 20:28:45
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 #include2 3 struct data { 4 long l; 5 int i; 6 int *pi; 7 char c; 8 9 short a[5]; 10 }; 11 12 int main(void) 13 { 14 struct data *dat; 15 16 dat = 0x100000; 17 18 printf("sizeof(struct data) = %d\n", sizeof(struct data)); 19 printf("dat = %p\n", dat); 20 printf("dat + 0x200 = %p\n", dat + 0x200); 21 printf("(unsigned long)dat + 0x200 = %p\n", (unsigned long)dat + 0x200); 22 printf("(char *)dat + 0x200 = %p\n", (char *)dat + 0x200); 23 24 return 0; 25 } /* 输出 sizeof(struct data) = 24 dat = 0x100000 dat + 0x200 = 0x103000 (unsigned long)dat + 0x200 = 0x100200 (char *)dat + 0x200 = 0x100200 */ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include int main(void) { char *array0[10][10]; char (*array1[10][10]); char (*array2)[10][10]; char (*array3[10])[10]; char (*array4)[][100]; char (*array5)[10][100]; printf("sizeof(array0) = %d\n", sizeof(array0)); printf("sizeof(array1) = %d\n", sizeof(array1)); printf("sizeof(array2) = %d\n", sizeof(array2)); printf("sizeof(array3) = %d\n", sizeof(array3)); printf("sizeof(array4) = %d\n", sizeof(array4)); printf("sizeof(array5) = %d\n", sizeof(array5)); return 0; } /* 输出 sizeof(array0) = 400 sizeof(array1) = 400 sizeof(array2) = 4 sizeof(array3) = 40 sizeof(array4) = 4 sizeof(array5) = 4 */ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 #include 2 #include 3 4 int main(void) 5 { 6 char a[20]; 7 strcpy(&a[0], "ces"); 8 strcpy(&a[1], "adfdfa"); 9 strcpy(&a[2], "hgfa"); 10 11 printf("%s\n", a); 12 13 return 0; 14 } /* 输出 cahgfa */ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 #include 2 #include 3 4 int main(void) 5 { 6 char a[] = "hello\0world"; 7 8 char b[20]; 9 10 memcpy(b, a, 12); 11 12 printf("strlen(a) = %d\n",strlen(a)); 13 printf("sizeof(b) = %d\n", sizeof(b)); 14 15 return 0; 16 } /* 输出 strlen(a) = 5 sizeof(b) = 20 */ 编程题: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. 冒泡->从小到大排序 #include #include void sort(char *p, int len) { int i, j; char tmp; for(i = 0; i < len-1; ++i) { for(j = 0; j < len-i-1; ++j) { if(p[j] > p[j+1]) { tmp = p[j]; p[j] = p[j+1]; p[j+1] = tmp; } } } } int main(void) { int i; char a[] = {4, 2, 3, 8 ,7 ,6, 9, 0}; int len = sizeof(a); sort(a, len); for(i = 0; i < len; ++i) { printf("%d ", a[i]); } printf("\n"); return 0; } /* 输出 0 2 3 4 6 7 8 9 */ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 2. * 输出字符串中第一个未重复的字符, 如有字符串"dggadgcdp", * 结果应该输出'a'. */