简体   繁体   English

关于输出的说明

[英]Explanation about the output

Can anyone please give me the explanation of output 谁能给我输出的解释

#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]);
    }
}

Output: 输出:

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

Type of *a[0] is char and type of a[0] is char* (pointer). *a[0]类型为char ,而a[0]类型为char* (指针)。

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

Type of a is char*[] , sizeof(a) == (7 * sizeof(char*)) == 28 . 的类型achar*[] sizeof(a) == (7 * sizeof(char*)) == 28

Output: 输出:

28

a is an array of 7 pointers; a是7个指针的数组; each pointer has size 4 on your system. 每个指针在系统上的大小为4。 Thus the 28. 因此28。

1

*a is the same as a[0] , so a[0][0] , *a[0] , **a and (*a)[0] are all equivalent: it is the first character of the first string. *aa[0]相同,因此a[0][0]*a[0]**a(*a)[0]都等效:它是第一个字符串的第一个字符。

After that, you should get the seven lines. 在那之后,您应该获得七行。

This is first line
This is second line
This is third line
This is fourth line
This is fifth line

Wait, what? 等一下 Mmm... 嗯...

for(i=0;i<=sizeof(a[0]);i++){

seems utterly wrong to me: 对我来说似乎完全错误:

for(i=0;i<sizeof(a)/sizeof(a[0]);i++){

should be better. 应该更好。 Why? 为什么? i<=sizeof(a[0]) is i<=4 , giving 5 lines. i<=sizeof(a[0])i<=4 ,给出5行。

However, i<sizeof(a)/sizeof(a[0]); 但是, i<sizeof(a)/sizeof(a[0]); is i < 28/4 , which is 7, the number of elements in the array. i < 28/4 ,即7,即数组中元素的数量。

28 - try to print out sizeof(char*), it will be 4 (bytes) so if you have 7 string literals youll have 4*7 = 28 bytes occupied with your array of char* pointers 28-尝试打印出sizeof(char *),它将为4(字节),因此,如果您有7个字符串文字,则char *指针数组将占用4 * 7 = 28个字节

1 - *a[0] is a size of a single character at address specified by first pointer in your array. 1-* a [0]是数组中第一个指针指定的地址处单个字符的大小。

But what is weird that you're using sizeof(a[0]) to get number of your strings. 但是,您使用sizeof(a[0])来获取字符串数是很奇怪的。 sizeof(a[0]) is equal to sizeof(char*) which is again 4 (bytes). sizeof(a [0])等于sizeof(char *),又是4(字节)。 so your for cycle will print out 5 strings because it's equal to this: 因此您的for循环将打印出5个字符串,因为它等于:

for(i=0;i<=4;i++){

It will run 0,1,2,3, 4 included. 它将运行0、1、2、3、4。 To print out all strings you should use: 要打印出所有字符串,您应该使用:

for(i=0;i<sizeof(a)/sizeof(char*);i++){
    printf("%s\n",a[i]);
}

char* a[] is an array of pointers to strings char * a []是指向字符串的指针数组

sizeof will give the number of bytes that the array has but will not tell how many character pointers, to get that you need to divide the total size with the size of one pointer: sizeof将给出数组具有的字节数,但不会告诉您有多少个字符指针,要获得总大小除以一个指针的大小即可:

sizeof( a ) / sizeof( char * )

an alternative way is to add a NULL pointer in your array 另一种方法是在数组中添加一个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]);
}

This is an array of pointers. 这是一个指针数组。 In a 32 bit machine, a pointer is 32 bit in size (aka 4 byte). 在32位计算机中,指针的大小为32位(也称为4字节)。 In the first printf() you print the sum of the sizes of all the pointers inside the array. 在第一个printf()中,您将打印数组内部所有指针的大小之和。 You have 7 lines -> 7*32 = 224 bit = 28 byte. 您有7行-> 7 * 32 = 224位= 28字节。 In the second printf() you print the size of the first character of the first line. 在第二个printf()中,打印第一行第一个字符的大小。 A char type is 8bit (aka 1 byte in size). 字符类型为8位(也就是1个字节)。

a is array of 7 character pointers. a是7个字符指针的数组。 So sizeof(a) is 7*sizeof(int)=28. 因此sizeof(a)为7 * sizeof(int)= 28。
a[0] is a character pointer , so value at a[0] (*a[0]) is character whose size is 1. a [0]是字符指针,因此a [0](* a [0])的值是大小为1的字符。
a[0] is also character pointer which is of type int whose size is 4. So your loop runs from i=0 to i=4. a [0]也是类型为int的字符指针,其大小为4。因此,循环从i = 0到i = 4。 Hence printing 5 strings. 因此打印5个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM