C++ 如何从指针获取数组的长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15774534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the length of an array from a pointer?
提问by Anthony Raimondo
I'm having trouble finding the length of an pointer array. Let's say I have:
我无法找到指针数组的长度。假设我有:
char array[40] = "hello"; // size 40
int length = sizeof array / sizeof array[0]; // no problem returns 40
//How do I get the length of the array with only a pointer to the first element in that array?
//如何仅使用指向该数组中第一个元素的指针来获取数组的长度?
char* pchar = array;
//if
//如果
std::strlen(pchar); // this returns the length of 5... i want length 40
//if
//如果
int count = 0;
while(true)
{
if(*(pchar + count) == 'int count = 0;
while(true)
{
if(*(pchar + count) == 'void foo (T *p) {
// p is a pointer to T
}
' && *(pchar + count + 1) != 'template <unsigned N, typename T>
void foo (T (&p)[N]) {
// p is a reference to an array[N] of T
std::cout << "array has " << N << " elements" << std::endl;
std::cout << "array has "
<< sizeof(p)/sizeof(p[0])
<< " elements"
<< std::endl;
}
int main ()
{
int array[40];
char array2[25];
foo(array);
foo(array2);
return 0;
}
')
break;
count++;
}
') // returns 5...
break;
count++;
}
How do I get it to return length 40 just from a pointer to the first element in the array?
I found that I can do this.
如何让它仅从指向数组中第一个元素的指针返回长度 40?
我发现我可以做到这一点。
struct
{
int* int_Array;
int array_Size;
}
This returns 39, this is good but I feel like this can be buggy in some situations.
这将返回 39,这很好,但我觉得这在某些情况下可能会出错。
采纳答案by RichieHindle
You can't, I'm afraid. You need to pass the length of the array to anyone who needs it. Or you can use a std::array
or std::vector
or similar, which keep track of the length themselves.
你不能,恐怕。您需要将数组的长度传递给需要它的任何人。或者你可以使用 astd::array
或std::vector
类似的东西,它们自己跟踪长度。
回答by milleniumbug
C++ has proper string type:
C++ 有正确的字符串类型:
std::string
std::string
which you may find helpful here. Even if you're passing it to function that accepts const char*
, it has .c_str()
method that allows you to pass it to function that accept a pointer. If the other function needs to modify the string, you can use &str[0]
which is valid for many implementations of C++, and is required to work for C++11. Just make sure you resize() them to the correct size.
您可能会在此处找到帮助。即使您将它传递给接受指针的函数const char*
,它也具有.c_str()
允许您将其传递给接受指针的函数的方法。如果其他函数需要修改字符串,您可以使用&str[0]
which 对 C++ 的许多实现有效,并且需要对 C++11 工作。只需确保将它们调整大小()到正确的大小。
Some of the other containers in C++ are:
C++ 中的其他一些容器是:
std::array
(C++11) Array of constant size. Better than plain old C array, as it has size()
method.
std::vector
Dynamic array (Java ArrayList
equivalent)
std::array
(C++11) 大小恒定的数组。比普通的旧 C 数组更好,因为它有size()
方法。
std::vector
动态数组(JavaArrayList
等效项)
As for your question - there is no way to find size of a pointed array. How could you even do that? It's just a stupid pointer.
至于你的问题 - 无法找到指向数组的大小。你怎么能这样做?这只是一个愚蠢的指针。
回答by john
You can't. And the moral is, don't use pointers and arrays, use vectors. You can always get the size of a vector.
你不能。道德是,不要使用指针和数组,使用向量。你总是可以得到一个向量的大小。
回答by jxh
It is true that you cannot get the array size from a pointer to an element of the array.
确实,您无法从指向数组元素的指针获取数组大小。
If the reason you only have a pointer is because you are implementing a function that takes an array parameter as an argument like this:
如果您只有一个指针的原因是因为您正在实现一个将数组参数作为参数的函数,如下所示:
##代码##then you can use a template function instead to get the array size to the function.
那么您可以改用模板函数来获取函数的数组大小。
##代码##回答by Sirmyself
If you want to keep simple and not use #include <array.h>
, you can make a struct where you array sizes are. A little bit like:
如果您想保持简单而不是使用#include <array.h>
,您可以在数组大小所在的位置创建一个结构体。有点像:
That way, you can to whatever you want (like deleting your instances or something)
这样,您可以随心所欲(例如删除实例或其他内容)