定义字符数组如何更加有效

int main(int argc, char *argv[])
{
    char str1[][6]={"hello","world","apple"};
    char* str2[3]={"hello","world","apple"};
    printf("%d\n",sizeof(str1));//3*6=18
    printf("%d\n",sizeof(str2));//3*4=12
    printf("%s\n",*str1);
    printf("%s\n",*(str1+1));
    printf("%s\n",*(str1+2));

    printf("%s\n",*str2);
    printf("%s\n",*(str2+1));
    printf("%s\n",*(str2+2));
    return 0;
}

特别说明:str2这种字符数组定义更加灵活,且更节省内存空间!

猜你喜欢

转载自blog.csdn.net/komtao520/article/details/81740834