实验1.1
代码
1 #include <stdio.h> 2 #define N 4 3 4 void test1(){ 5 int a [N] = {1,9,8,4}; 6 int i; 7 8 printf("sizeof(a) = %d\n",sizeof(a)); 9 10 for (i = 0; i<N;++i) 11 printf("%p: %d\n",&a[i],a[i]); 12 13 printf("a=%p\n",a); 14 } 15 void test2(){ 16 char b[N]={'1','9','8','4'}; 17 int i; 18 printf("sizeof(b)=%d\n",sizeof(b)); 19 for (i=0;i<N;++i) 20 printf("%p:%c\n",&b[i],b[i]); 21 printf("b=%p\n",b); 22 23 } 24 25 int main(){ 26 printf("测试1:int类型一维数组\n"); 27 test1(); 28 29 printf("\n测试2:char类型一维数组\n"); 30 test2(); 31 32 return 0; 33 }
截图
问题
1:不连续,16,不一样
2.连续,4,不一样
实验1.2
代码
1 #include <stdio.h> 2 #define N 2 3 #define M 4 4 5 void test1(){ 6 int a[N][M] = {{1,9,8,4},{2,0,4,9}}; 7 int i,j; 8 9 printf("sizeof(a) = %d\n",sizeof(a)); 10 11 for (i = 0; i<N;++i) 12 for(j=0;i<N;++i) 13 printf("%p: %d\n",&a[i][j],a[i][j]); 14 15 printf("\n"); 16 printf("a=%p\n",a); 17 printf("a[0]=%p\n",a[0]); 18 printf("a[1]=%p\n",a[1]); 19 printf("\n"); 20 } 21 void test2(){ 22 char b [N][M] ={{'1','9','8','4'},{'2','0','4','9'}}; 23 int i ,j; 24 printf("sizeof(b)=%d\n",sizeof(b)); 25 for (i=0;i<N;++i) 26 for(j=0;j<M;++j) 27 printf("%p:%c\n",&b[i][j],b[i][j]); 28 printf("\n"); 29 printf("b=%p\n",b); 30 printf("b[0]=%p\n",b[0]); 31 printf("b[1]=%p\n",b[1]); 32 33 34 } 35 36 int main(){ 37 printf("测试1:int类型一维数组\n"); 38 test1(); 39 40 printf("\n测试2:char类型一维数组\n"); 41 test2(); 42 43 return 0; 44 }
截图
问题
1.
实验2
代码
1 #include<stdio.h> 2 #include<string.h> 3 4 #define N 80 5 6 void swap_str(char s1[N],char s2[N]); 7 void test1(); 8 void test2(); 9 10 int main(){ 11 printf("测试1;用两个一维char数组,实现两个字符交换\n"); 12 test1(); 13 14 printf("\n测试2;用二维char数组,实现两个字符串交换\n"); 15 test2(); 16 return 0; 17 } 18 19 void test1(){ 20 char views1[N]="hey,C,I hate u."; 21 char views2[N]="hey,C,I love u."; 22 23 printf("交换前;\n"); 24 puts(views1); 25 puts(views2); 26 27 swap_str(views1,views2); 28 29 printf("交换后; \n"); 30 puts(views1); 31 puts(views2); 32 33 swap_str(views1,views2); 34 35 printf("交换后; \n"); 36 puts(views1); 37 puts(views2); 38 39 swap_str(views1,views2); 40 41 printf("交换后; \n"); 42 puts(views1); 43 puts(views2); 44 45 } 46 void test2(){ 47 char views[2][N]={"hey,c,I hate u.", 48 "hey,c,I love u."}; 49 printf("交换前; \n"); 50 puts(views[0]); 51 puts(views[1]); 52 swap_str(views[0],views[1]); 53 printf("交换后; \n"); 54 puts(views[0]); 55 puts(views[1]); 56 57 58 } 59 void swap_str(char s1[N],char s2[N]){ 60 char tmp[N]; 61 strcpy(tmp,s1); 62 strcpy(s1,s2); 63 strcpy(s2,tmp); 64 }
截图、
标签:puts,void,char,views1,views2,实验,printf From: https://www.cnblogs.com/z00z/p/17829618.html