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("printf arrU8[12] by (char*)arrU8=%s\n", (char*)arrU8);
printf("sizeof(arrU8)=%d\n", sizeof(arrU8));
printf("sizeof((char*)arrU8)=%d\n", sizeof((char*)arrU8));
printf("strlen(arrU8)=%d\n", strlen(arrU8));
printf("strlen((char*)arrU8)=%d\n", strlen((char*)arrU8));
memcpy(arrU8, "123456789", 9);
printf("---------------------------------------------\n");
printf("printf arrU8[12] by arrU8 = %s\n", arrU8);
printf("printf arrU8[12] by (char*)arrU8=%s\n", (char*)arrU8);
printf("sizeof(arrU8)=%d\n", sizeof(arrU8));
printf("sizeof((char*)arrU8)=%d\n", sizeof((char*)arrU8));
printf("strlen(arrU8)=%d\n", strlen(arrU8));
printf("strlen((char*)arrU8)=%d\n", strlen((char*)arrU8));
printf("---------------------------------------------\n");
char arrCh[12] = {0};
printf("printf arrCh[12] by arrCh = %s\n", arrCh);
printf("printf arrCh[12] by (char*)arrCh=%s\n", (char*)arrCh);
printf("sizeof(arrCh)=%d\n", sizeof(arrCh));
printf("sizeof((char*)arrCh)=%d\n", sizeof((char*)arrCh));
printf("strlen(arrCh)=%d\n", strlen(arrCh));
printf("strlen((char*)arrCh)=%d\n", strlen((char*)arrCh));
memcpy(arrCh, "987", 3);
printf("---------------------------------------------\n");
printf("printf arrCh[12] by arrCh = %s\n", arrCh);
printf("printf arrCh[12] by (char*)arrCh=%s\n", (char*)arrCh);
printf("sizeof(arrCh)=%d\n", sizeof(arrCh));
printf("sizeof((char*)arrCh)=%d\n", sizeof((char*)arrCh));
printf("strlen(arrCh)=%d\n", strlen(arrCh));
printf("strlen((char*)arrCh)=%d\n", strlen((char*)arrCh));
printf("---------------------------------------------\n");
printf("arrU8=%s strlen(arrU8)=%d\n", arrU8, strlen(arrU8));
memset(arrU8, 0, sizeof(arrU8));
memcpy(arrU8, arrCh, strlen(arrCh));
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