其他分享
首页 > 其他分享> > C语言第12天,数组(一)

C语言第12天,数组(一)

作者:互联网

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>

int main() {

	int arr[10] = { 1,2,3 };//不完全初始化,剩下的元素默认初始化为0
	char arr2[5] = { 'a','b' };
	char arr3[5] = "abc";//ok
	char arr4[5] = { 'a',98 };//ok,输入数字会认为是当前数字的ASCII对应的字符
	//int n = 5;
	//char ch[n];//err

	printf("%d\n", sizeof(arr3));//5  sizeof计算所占空间的大小
	//arr3设定了元素数量,所以所占空间永远是5
	printf("%d\n", strlen(arr3));//3  strlen求字符串的长度

	printf("------------------------\n");

	char arr5[] = "abcde";
	printf("%d\n", sizeof(arr5));//6 -- 包括了/0
	printf("%d\n", strlen(arr5));//5
	return 0;
}
5
3
------------------------
6
5
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>


int main() {
	//打印数组每个元素的地址
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int i = 0;
	for (i = 0; i < sz; i++) {
		printf("&arr[%d] = %p\n", i,&arr[i]);
	}

	printf("-------------------------------\n");

	//二维数组
	int arr2[][4] = { {1,2,3,4},{5,6,7,8} };//行可以省略,列不能省略

	int arr3[3][4] = { {1,2,3},{4,5,} };

	int j = 0;
	for (j = 0; j < 3; j++) {
		int k = 0;
		for (k = 0; k < 4; k++) {
			//打印元素
			printf("%d",arr3[j][k]);
		}
		printf("\n");
	}
	printf("-------------------------------\n");
	int j2 = 0;
	for (j2 = 0; j2 < 3; j2++) {
		int k2 = 0;
		for (k2 = 0; k2 < 4; k2++) {
			//打印元素地址
			printf("&arr3[%d][%d] = %p\n", j2, k2, &arr3[j2][k2]);
		}
	}
	return 0;
}
&arr[0] = 00B9F750
&arr[1] = 00B9F754
&arr[2] = 00B9F758
&arr[3] = 00B9F75C
&arr[4] = 00B9F760
&arr[5] = 00B9F764
&arr[6] = 00B9F768
&arr[7] = 00B9F76C
&arr[8] = 00B9F770
&arr[9] = 00B9F774
-------------------------------
1230
4500
0000
-------------------------------
&arr3[0][0] = 00B9F6D8
&arr3[0][1] = 00B9F6DC
&arr3[0][2] = 00B9F6E0
&arr3[0][3] = 00B9F6E4
&arr3[1][0] = 00B9F6E8
&arr3[1][1] = 00B9F6EC
&arr3[1][2] = 00B9F6F0
&arr3[1][3] = 00B9F6F4
&arr3[2][0] = 00B9F6F8
&arr3[2][1] = 00B9F6FC
&arr3[2][2] = 00B9F700
&arr3[2][3] = 00B9F704

标签:arr,12,int,j2,C语言,k2,arr3,数组,printf
来源: https://blog.csdn.net/wdnmd____c/article/details/118549105