简体   繁体   English

为什么编译器没有在参数中传递数组char * arr []的大小?

[英]Why compiler doesn't pass size of array char *arr[] in parameters?

Why compiler doesn't pass size of array char *arr[] in parameters? 为什么编译器没有在参数中传递数组char * arr []的大小? I wanted to get get size of array passed by parameter but I guess it doesn't work because even char *a[] is char ** my question is why is it and can I make it work? 我想获得通过参数传递的数组的大小,但我想它不起作用,因为即使char *a[]char **我的问题是为什么它,我能使它工作吗?

#include <stdio.h>
#include <stddef.h>
#include <stdio.h>

template<class T, size_t len>
constexpr size_t lengthof(T(&)[len])
{
    return len;
}
void printarr(const char *a[]);

int main()
{
    const char *a[] = { "aba", "bd", "cd" };
    printarr(a);
}

void printarr(const char *a[])
{
    for(size_t i = 0, c = lengthof(a); i < c; i++) {
        printf("str = %s\n", a[i]);
    }
}

You can make it work by using the same trick you used in your lengthof function template. 您可以使用在lengthof函数模板中使用的相同技巧使其工作。

template<size_t len>
void printarr(const char* (&a)[len])
{
    for(size_t i = 0, c = lengthof(a); i < c; i++) {
        printf("str = %s\n", a[i]);
    }
}

That has been a feature of C since the beginning and carried through into C++. 这从一开始就是C的一个特性,并贯穿到C ++中。

In the case of string arrays, the solution has been to add a trailing null character to mark the end. 对于字符串数组,解决方案是添加尾随空字符以标记结尾。

The was probably done for the sake of efficiency when C started on ancient PDP computers. 当C在古老的PDP计算机上开始时,可​​能是为了效率而做的。

Use strlen; 使用strlen; or better yet std::string or std::vector. 或者更好的是std :: string或std :: vector。

Because this is required by the C++ standard, which inherited this behavior from C (and since C++ wants to stay compatible with C). 因为这是C ++标准所要求的,它从C继承了这种行为(因为C ++希望与C保持兼容)。

As function parameters, arrays are decayed into pointers. 作为函数参数,数组被衰减为指针。

You really want to use something like std::vector<std::string> instead. 你真的想要使用类似std::vector<std::string> Learn more about standard C++ STL containers . 了解有关标准C ++ STL容器的更多信息。

C++ is a language what provides low level access to system resources. C ++是一种提供对系统资源的低级访问的语言。 Processor is not working with objects, but with memory addresses. 处理器不使用对象,但使用内存地址。

If you want predefined structures, use Java, C#, python, or c++ libs like stl. 如果您想要预定义的结构,请使用Java,C#,python或c ++库,例如stl。

From the point of view of the language, your array doesn't have a length; 从语言的角度来看,你的数组没有长度; the compiler cannot read your mind and decide where the array begins and where it ends, and it cannot waste memory and processing to perform range checking. 编译器无法读取您的想法并决定数组的开始位置和结束位置,并且不会浪费内存和处理来执行范围检查。
The closest thing to an array length is the number of consecutive memory spaces that have been allocated by a single new[] invocation, but such information is only authoritative for the purpose of allocating and deallocating memory: the "array" you want to process in your function might be only a portion of a single allocation (for example, you might allocate memory to load a whole text file and process each line separately). 与数组长度最接近的是由单个new[]调用分配的连续内存空间的数量,但是这些信息仅用于分配和释放内存的权威:要在其中处理的“数组”您的函数可能只是单个分配的一部分(例如,您可能会分配内存来加载整个文本文件并分别处理每一行)。

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

相关问题 为什么编译器不抱怨将char添加到char *? - Why doesn't the compiler complain about adding char to char*? 在命令行参数中为什么我们不能使用 string *arr 而不是 char **arr - In command line arguments why can't we use string *arr instead of char **arr 为什么在零数组的情况下编译器不推导template参数? - Why a compiler doesn't deduce the template parameter in case of zero array? for( auto &amp;&amp; v: arr ) 不会破坏数组的内容吗? - Doesn't for( auto && v : arr ) clobber the contents of the array? 将char数组复制到结构中时,为什么memcpy不起作用? - Why doesn't memcpy work when copying a char array into a struct? 为什么这个函数不打印所有的字符数组呢? - Why doesn't this function print all the char array as it takes it? 当vector <char> &&绑定到vector <char>&时,编译器不会抱怨 - The compiler doesn't complain when vector<char>&& is bound to vector<char>& 为什么arr [-2]不等于-2 [arr]? - Why isn't arr[-2] equivalent to -2[arr]? 为什么我们不能直接将 arr 分配给指向数组的指针 - Why can't we directly assign arr to pointer to array 为什么下一个类为什么不对数组arr做浅拷贝? - Why isn't the following class doing shallow copy for the array arr?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM