test_sizeof

test_sizeof


//结论:
// sizeof(arrU8)得到的大小是u8类型数组的 **定义大小**,在 初始化的时候用
// strlen(arrU8)得到的大小是u8类型数组的 **实际大小**,在 复制的时候用
//sizeof((char*)arrU8),把一个u8 * 转成 char *,在32位机器上结果是4,64位机器上是8
//sizeof((char*)arrU8)是输出一个指针的大小
//要打印u8类型的数组,直接arrU8就可以了,不用加(char*);

#include 
#include 
#include 

typedef unsigned char u8;


int main(void)
{
	u8 arrU8[12] = {0};
	printf("printf arrU8[12] by arrU8 = %s\n", arrU8);//printf arrU8[12] by arrU8 = 
	printf("printf arrU8[12] by (char*)arrU8=%s\n", (char*)arrU8);//printf arrU8[12] by (char*)arrU8=
	printf("sizeof(arrU8)=%d\n", sizeof(arrU8));//sizeof(arrU8)=12
	printf("sizeof((char*)arrU8)=%d\n", sizeof((char*)arrU8));//sizeof((char*)arrU8)=8
	printf("strlen(arrU8)=%d\n", strlen(arrU8));//strlen(arrU8)=0
	printf("strlen((char*)arrU8)=%d\n", strlen((char*)arrU8));//strlen((char*)arrU8)=0
	
	memcpy(arrU8, "123456789", 9);
	printf("---------------------------------------------\n");
	printf("printf arrU8[12] by arrU8 = %s\n", arrU8);//printf arrU8[12] by arrU8 = 123456789
	printf("printf arrU8[12] by (char*)arrU8=%s\n", (char*)arrU8);//printf arrU8[12] by (char*)arrU8=123456789
	printf("sizeof(arrU8)=%d\n", sizeof(arrU8));//sizeof(arrU8)=12
	printf("sizeof((char*)arrU8)=%d\n", sizeof((char*)arrU8));//sizeof((char*)arrU8)=8
	printf("strlen(arrU8)=%d\n", strlen(arrU8));//strlen(arrU8)=9
	printf("strlen((char*)arrU8)=%d\n", strlen((char*)arrU8));//strlen((char*)arrU8)=9
	//结论:
	//sizeof(arrU8)得到的大小是u8类型数组的定义大小
	//strlen(arrU8)得到的大小是u8类型数组的实际大小
	//sizeof((char*)arrU8),把一个u8 * 转成 char *,在32位机器上结果是4,64位机器上是8
	//sizeof((char*)arrU8)是输出一个指针的大小
	//要打印u8类型的数组,直接arrU8就可以了,不用加(char*);
	printf("---------------------------------------------\n");
	
	char arrCh[12] = {0};
	printf("printf arrCh[12] by arrCh = %s\n", arrCh);//printf arrCh[12] by arrCh = 
	printf("printf arrCh[12] by (char*)arrCh=%s\n", (char*)arrCh);//printf arrCh[12] by (char*)arrCh=
	printf("sizeof(arrCh)=%d\n", sizeof(arrCh));//sizeof(arrCh)=12
	printf("sizeof((char*)arrCh)=%d\n", sizeof((char*)arrCh));//sizeof((char*)arrCh)=8
	printf("strlen(arrCh)=%d\n", strlen(arrCh));//strlen(arrCh)=0
	printf("strlen((char*)arrCh)=%d\n", strlen((char*)arrCh));//strlen((char*)arrCh)=0
	
	memcpy(arrCh, "987", 3);
	printf("---------------------------------------------\n");
	printf("printf arrCh[12] by arrCh = %s\n", arrCh);//printf arrCh[12] by arrCh = 123456789
	printf("printf arrCh[12] by (char*)arrCh=%s\n", (char*)arrCh);//printf arrCh[12] by (char*)arrCh=123456789
	printf("sizeof(arrCh)=%d\n", sizeof(arrCh));//sizeof(arrCh)=12
	printf("sizeof((char*)arrCh)=%d\n", sizeof((char*)arrCh));//sizeof((char*)arrCh)=8
	printf("strlen(arrCh)=%d\n", strlen(arrCh));//strlen(arrCh)=9
	printf("strlen((char*)arrCh)=%d\n", strlen((char*)arrCh));//strlen((char*)arrCh)=9

	printf("---------------------------------------------\n");
	printf("arrU8=%s strlen(arrU8)=%d\n", arrU8, strlen(arrU8));
	memset(arrU8, 0, sizeof(arrU8));//初始化的时候用sizeof
	memcpy(arrU8, arrCh, strlen(arrCh));//复制的时候strlen
	printf("arrU8=%s strlen(arrU8)=%d\n", arrU8, strlen(arrU8));
	
	printf("---------------------------------------------\n");
	printf("arrU8=%s strlen(arrU8)=%d\n", arrU8, strlen(arrU8));
	memset(arrU8, 0, sizeof(arrU8));
	memcpy(arrU8, "54321", 5);
	printf("arrU8=%s strlen(arrU8)=%d\n", arrU8, strlen(arrU8));

	printf("---------------------------------------------\n");
	printf("arrCh=%s strlen(arrCh)=%d\n", arrCh, strlen(arrCh));
	memset(arrCh, 0, sizeof(arrCh));
	memcpy(arrCh, arrU8, strlen(arrU8));
	printf("arrCh=%s strlen(arrCh)=%d\n", arrCh, strlen(arrCh));

	exit(0);
}
printf arrU8[12] by arrU8 = 
printf arrU8[12] by (char*)arrU8=
sizeof(arrU8)=12
sizeof((char*)arrU8)=8
strlen(arrU8)=0
strlen((char*)arrU8)=0
---------------------------------------------
printf arrU8[12] by arrU8 = 123456789
printf arrU8[12] by (char*)arrU8=123456789
sizeof(arrU8)=12
sizeof((char*)arrU8)=8
strlen(arrU8)=9
strlen((char*)arrU8)=9
---------------------------------------------
printf arrCh[12] by arrCh = 
printf arrCh[12] by (char*)arrCh=
sizeof(arrCh)=12
sizeof((char*)arrCh)=8
strlen(arrCh)=0
strlen((char*)arrCh)=0
---------------------------------------------
printf arrCh[12] by arrCh = 987
printf arrCh[12] by (char*)arrCh=987
sizeof(arrCh)=12
sizeof((char*)arrCh)=8
strlen(arrCh)=3
strlen((char*)arrCh)=3
---------------------------------------------
arrU8=123456789 strlen(arrU8)=9
arrU8=987 strlen(arrU8)=3
---------------------------------------------
arrU8=987 strlen(arrU8)=3
arrU8=54321 strlen(arrU8)=5
---------------------------------------------
arrCh=987 strlen(arrCh)=3
arrCh=54321 strlen(arrCh)=5

你可能感兴趣的:(Linux,linux)