简体   繁体   English

strlen 为二维数组返回错误值

[英]strlen returning wrong value for 2d array

Why is this:为什么是这样:

char animals[][30] = {{"cow "}, {"dog "}, {"frog "}, {"gecko "}, {"cat"}};

returning 4 instead of 5 when I do this:当我这样做时返回 4 而不是 5:

printf("%d\n", strlen(animals));

From what I believe, there are 5 different arrays of character arrays so i'm not sure why it is saying there are 4.据我所知,字符 arrays 有 5 个不同的 arrays,所以我不确定为什么它说有 4 个。

This declaration本声明

char animals[][30] = {{"cow "}, {"dog "}, {"frog "}, {"gecko "}, {"cat"}};

does not declare a one-dimensiobal array that contains a string.不声明包含字符串的一维数组。

The type of the argument of the function strlen shall be char * . function strlen的参数类型应为char * But you are using an argument of the type char ( * )[30] .但是您使用的是char ( * )[30]类型的参数。

What you need is the following您需要的是以下内容

printf( "%zu\n", sizeof( animals ) / sizeof( *animals ) );

i'm not sure why it is saying there are 4.我不知道为什么它说有 4 个。

Because in this call of printf that uses an invalid conversion specifier %d instead of %zu因为在printf的这个调用中使用了无效的转换说明符%d而不是%zu

printf("%d\n", strlen(animals));

the value of the argument animals in the call调用中参数animals的值

strlen(animals)

is considered as the address of the first element of the array that indeed has the length equal to 4 .被认为是长度确实等于4的数组的第一个元素的地址。

That is from the point of view of equality of addresses this call那是从这个调用的地址平等的角度来看

strlen(animals)

is equivalent to the call相当于调用

strlen(animals[0])

because the value of animals used in the first call is equal to the value of the address of its first element animals[0] .因为第一次调用中使用的animals的值等于其第一个元素animals[0]的地址值。 Though in the first call of strlen the argument has an invalid type.尽管在第一次调用strlen时,参数的类型无效。

The compiler should issue a message that you are using an incorrect argument in the call of strlen .编译器应该发出一条消息,表明您在strlen的调用中使用了不正确的参数。

strlen() is for calculating the length of strings (null-terminated sequence of characters in C), not for calculating the number of elements of array. strlen()用于计算字符串的长度(C 中以空字符结尾的字符序列),而不是用于计算数组元素的数量。

To obtain the number of elements in an array, you can divide the size of array by the size of an element of the array.要获得数组中元素的数量,可以将数组的大小除以数组元素的大小。

printf("%zu\n", sizeof(animals) / sizeof(*animals));

Also note that you should use %zu , not %d , to print size_t ( strlen() and sizeof return size_t )另请注意,您应该使用%zu而不是%d来打印size_tstrlen()sizeof return size_t

There are multiple issues with the call printf("%d\n", strlen(animals));调用printf("%d\n", strlen(animals));存在多个问题。

  • the printf format should be %zu if strlen has been properly declared by including <string.h> at the point of the call.如果通过在调用点包含<string.h>正确声明了strlen ,则printf格式应为%zu This is unlikely to pose a problem but should be fixed.这不太可能造成问题,但应该解决。
  • strlen() takes a C String and animal is not a C string, it is a 2D array of char . strlen()采用 C 字符串,而animal不是 C 字符串,它是char的二维数组。 You should instead pass strlen(animal[0]) .你应该通过strlen(animal[0]) Again this mistake is unlikely to pose a problem because the address of the first string in the 2D array is the same as that of the array itself.同样,这个错误不太可能造成问题,因为二维数组中第一个字符串的地址与数组本身的地址相同。 To compute the number of elements in the 2D array, use sizeof(animal) / sizeof(*animal) .要计算二维数组中的元素数量,请使用sizeof(animal) / sizeof(*animal)
  • the call should print 4 (the string "cow " has a trailing space) and a newline, and the return value will be 2 as the number of characters output by printf .该调用应打印4 (字符串"cow "有一个尾随空格)和一个换行符,返回值将是2作为 output 的字符数printf

Try this modified version:试试这个修改后的版本:

#include <stdio.h>
#include <string.h>

int main() {
    char animals[][30] = {{"cow "}, {"dog "}, {"frog "}, {"gecko "}, {"cat"}};

    printf("number of strings: %zu\n", sizeof(animals) / sizeof(*animals));
    for (size_t i = 0; i < sizeof(animals) / sizeof(*animals); i++) {
        printf("%zu: \"%s\", %zu chars\n", i, animals[i], strlen(animals[i]));
    }
    return 0;
}

Basic answer to your question您的问题的基本答案

"returning 4 instead of 5 when I do this" “当我这样做时返回 4 而不是 5”

when you do this当你这样做时

printf("%d\n", strlen(animals));

animals is a pointer to the first of the strings, animals is stored in memory as "cow \0........dog \0..." where..... are bytes until the next array entry (usually random memory stuff if not initialized).动物是指向第一个字符串的指针,动物在 memory 中存储为“cow \0........dog \0...” 其中.....是直到下一个数组条目的字节(如果未初始化,通常是随机的 memory 东西)。 When you printf strlen of animals it returns strlen of "cow ".当你 printf strlen 动物它返回 strlen 的“牛”。

You are mixing types.. c allows this because they are pointers really, this is why compiler works but it is wrong.您正在混合类型.. c 允许这样做,因为它们确实是指针,这就是编译器可以工作但错误的原因。

to get 5 as result you will need to do this要获得 5 作为结果,您需要执行此操作

printf("%zu",sizeof(animals) /sizeof(animals[0]));

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

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