指针类型分析

/* 指针类型分析 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>

void test()
{
    const char* p1 = "world";
    const char p2[] = "world";

    printf("const char* p1: %ld\n", sizeof(p1));    // 打印 8
    printf("char p2[]: %ld\n", sizeof(p2));         // 打印6
    printf("char p2[]: %ld\n", sizeof("world"));    // 打印6

    /*
    思考分析:
        从现象观察,sizeof计算的是数据类型大小,使用sizeof一定要注意形参的数据类型
        我的错误认知: 我以前认为p1,p2数据类型都是char*,从测试来看char*和char []的确不一样

        另外字符串字面量"world"的真实类型应该是字符串数组
    */
}

int main()
{
    test();
    return 0;
}

 

posted on 2023-02-04 19:31  寒魔影  阅读(15)  评论(0编辑  收藏  举报

导航