1. 程式人生 > >定義字元陣列如何更加有效

定義字元陣列如何更加有效

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這種字元陣列定義更加靈活,且更節省記憶體空間!