1

出力の説明を誰か教えてください

#include<stdio.h>
int main(void){
    int i;
    char *a[]={ "This is first line",
                "This is second line",
                "This is third line",
                "This is fourth line",
                "This is fifth line",
                "This is sixth line",
                "This is seventh line end"};
    printf("%d\n",sizeof(a));
    printf("%d\n",sizeof(*a[0]));
    for(i=0;i<=sizeof(a[0]);i++){
        printf("%s\n",a[i]);
    }
}

出力:

28
1
This is first line
This is second line
This is third line
This is fourth line
This is fifth line
4

6 に答える 6

3

is の型と isの*a[0]char(ポインター)。a[0]char*

sizeof(char) == 1sizeof(char*) == 4

のタイプachar*[]sizeof(a) == (7 * sizeof(char*)) == 28です。

于 2013-10-28T10:02:57.517 に答える
2

char* a[] は文字列へのポインタの配列です

sizeof は配列のバイト数を示しますが、文字ポインターの数はわかりません。合計サイズを 1 つのポインターのサイズで割る必要があることを取得するには、次のようにします。

sizeof( a ) / sizeof( char * )

別の方法は、配列に NULL ポインターを追加することです

char *a[]={ "This is first line",
            "This is second line",
            "This is third line",
            "This is fourth line",
            "This is fifth line",
            "This is sixth line",
            "This is seventh line end",
            NULL };
printf("%d\n",sizeof(a));
printf("%d\n",sizeof(*a[0]));
for(i=0; a[i] != NULL;i++){
    printf("%s\n",a[i]);
}
于 2013-10-28T10:10:41.763 に答える
0

これはポインタの配列です。32 ビット マシンでは、ポインターのサイズは 32 ビット (別名 4 バイト) です。最初の printf() では、配列内のすべてのポインターのサイズの合計を出力します。7 行あります -> 7*32 = 224 ビット = 28 バイト。2 番目の printf() では、最初の行の最初の文字のサイズを出力します。char 型は 8 ビット (サイズは 1 バイト) です。

于 2013-10-28T10:20:17.497 に答える