[英]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.