赞
踩
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- { //定义变量
- int m = 3;
- int n = 4;
- //定义二维数组
- int a[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
- //遍历数组
- for(m = 0; m < 3; m++)
- {
- for(n = 0; n < 4; n++)
- {
- printf("a[%d][%d] = %d ",m,n,a[m][n]);
- }
- printf("\n");
- }
- system("pause");
- return 0;
- }
测试结果:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- { //定义变量
- int m = 3;
- int n = 4;
- //定义二维数组
- int a[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
- printf("二维数组元素\n");
- //遍历数组
- for(m = 0; m < 3; m++)
- {
- for(n = 0; n < 4; n++)
- {
- printf("a[%d][%d] = %d ",m,n,a[m][n]);
- }
- printf("\n");
- }
-
- printf("\n\n");
- //取出二维数组元素的地址
- printf("二维数组首地址为:&a = %p\n\n",a);
-
- //二维数组的地址
-
- printf("二维数组的地址\n");
- int i,j;
- for(i = 0; i < 3; i++)
- {
- for(j = 0; j < 4; j++)
- {
- //二维数组的元素的地址
- printf("&a[%d][%d] = %p ",i,j,&a[i][j]);
- }
- printf("\n");
- }
-
- system("pause");
- return 0;
- }
测试结果:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- //定义一个数组
- int a[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
- //计算数组长度
- printf("二维数组a[3][4]的长度为:%d\n",sizeof(a));
- system("pause");
- return 0;
- }
测试结果:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- //定义字符数组
- char s[6] = {'a','b','c','d','e','f'};
- int i;
- //遍历字符数组
- for(i = 0; i < sizeof(s); i++)
- {
- printf("s[%d] = %c\n",i,s[i]);
- }
- printf("\n");
- //定义字符数组并初始化为字符串
- char str[7] = "abcdef";
- int j;
- //遍历字符串
- for(j = 0; j < sizeof(str); j++)
- {
- //计算数组长度
- printf("str[%d] = %c\n",j,str[j]);
- }
- printf("\n");
- //以字符串格式输出
- printf("以字符串格式输出为:%s\n",str);
- system("pause");
- return 0;
- }
测试结果:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- //定义字符数组
- char s[] = "HeiMaXunLianYin";
- //输出字符串
- printf(" %s\n",s);
- printf("\n");
- //从给定地址输出
- printf(" %s\n",&s[5]);
- printf("\n");
-
- //定义一个字符串
- char s1[] = "HeiMa\0XunLianYin";
- // 直到遇到\0结束测试
- printf(" %s\n",s1);
- system("pause");
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- //定义字符数组
- char s[] = {'a','b','c','d'};
- //输出字符串
- printf(" %s\n",s);
- printf("\n");
- //从给定地址输出
- printf(" %s\n",&s[2]);
- printf("\n");
-
- //定义一个字符串
- char s1[] = {'a','b','c','d','\0'};
- // 直到遇到\0结束测试
- printf(" %s\n",s1);
- //从给定地址输出
- printf(" %s\n",&s1[2]);
- system("pause");
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- //定义字符数组
- char s[]={'0'};
- //输出字符串
- printf("请输入一个字符串\n",s);
- //接受键盘输入的值
- scanf("%s",s);
- //打印输入的字符串
- printf(" 您输入的字符串为:%s\n",s);
-
- system("pause");
- return 0;
- }
测试结果1:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。