/**---------------------------------------------------------------
* @brief 指针和数组变量sizeof()和strlen()的比较
*
*---------------------------------------------------------------*/
int point_array_init()
{
int *PointOne = "123456";
char *PointTwo = "123456";
char *PointThree = "123456\0";
int ArrayOne[6] = {1,2,3,4,5,6};
char ArrayTwo[6] = {1,2,3,4,5,6};
char ArrayThree[7] = {1,2,3,4,5,6,'\0'};
printf("\n\t|-------------------------------------------------------------------------\n");
printf("\t| int *Point: | ");
printf("sizeof(Point) = %d | ",sizeof(PointOne));
printf("strlen(Point) = %d\n",strlen(PointOne));
printf("\t|--------------------|---------------------------|------------------------\n");
printf("\t| char *Point: | ");
printf("sizeof(Point) = %d | ",sizeof(PointTwo));
printf("strlen(Point) = %d\n",strlen(PointTwo));
printf("\t|--------------------|---------------------------|------------------------\n");
printf("\t| char *Point_0: | ");
printf("sizeof(Point_0) = %d | ",sizeof(PointThree));
printf("strlen(Point_0) = %d\n",strlen(PointThree));
printf("\t|====================*===========================*========================\n");
printf("\t| int Array[x]: | ");
printf("sizeof(Array) = %d | ",sizeof(ArrayOne));
printf("strlen(Array) = %d\n",strlen(ArrayOne));
printf("\t|--------------------|---------------------------|------------------------\n");
printf("\t| char Array[x]: | ");
printf("sizeof(Array) = %d | ",sizeof(ArrayTwo));
printf("strlen(Array) = %d\n",strlen(ArrayTwo));
printf("\t|--------------------|---------------------------|------------------------\n");
printf("\t| char Array_0[x]: | ");
printf("sizeof(Array_0) = %d | ",sizeof(ArrayThree));
printf("strlen(Array_0) = %d\n",strlen(ArrayThree));
printf("\t|-------------------------------------------------------------------------\n");
return 0;
}
打印数据:
|-------------------------------------------------------------------------
| int *Point: | sizeof(Point) = 4 | strlen(Point) = 6
|--------------------|---------------------------|------------------------
| char *Point: | sizeof(Point) = 4 | strlen(Point) = 6
|--------------------|---------------------------|------------------------
| char *Point_0: | sizeof(Point_0) = 4 | strlen(Point_0) = 6
|====================*===========================*========================
| int Array[x]: | sizeof(Array) = 24 | strlen(Array) = 1
|--------------------|---------------------------|------------------------
| char Array[x]: | sizeof(Array) = 6 | strlen(Array) = 6
|--------------------|---------------------------|------------------------
| char Array_0[x]: | sizeof(Array_0) = 7 | strlen(Array_0) = 6
|-------------------------------------------------------------------------
结论:
(1) sizeof 就是返回一个对象或者类型所占的内存字节数(包括'\0')
(2) strlen函数求的是字符串的实际长度,它求得方法是从开始到遇到第一个'\0'即停止(不包括'\0');
(3) strlen(char *s),传递的参数是char *类型,不是int *类型。
来源:CSDN
作者:my___sunshine
链接:https://blog.csdn.net/my___sunshine/article/details/104041507