指针和数组计算大小(sizeof()和strlen()的对比)

醉酒当歌 提交于 2020-01-20 01:34:14
/**---------------------------------------------------------------
 * @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 *类型。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!