C++ 查找 int [] 数组的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17920885/
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
Finding the size of int [] array
提问by Sabeer Ebrahim
In the following function, how can we find the length of the array
在下面的函数中,我们如何找到数组的长度
int fnLenghthOfArray(int arry[]){
return sizeof(arry)/sizeof(int); // This always returns 1
}
Here this function always returns 1.
Where as, sizeof(arry)/sizeof(int)
gives the actual length of the array, in the function where it is declared.
此处此函数始终返回 1。其中sizeof(arry)/sizeof(int)
,在声明它的函数中,给出数组的实际长度。
If we use vector or template like
如果我们使用矢量或模板
template<typename T,int N>
int fnLenghthOfArray(T (&arry)[N]){
}
we can get the size. But here I am not allowed to change the function prototype.
我们可以得到尺寸。但是在这里我不允许更改函数原型。
Please help me to find this.
请帮我找到这个。
回答by Maroun
Remember, in C when you pass an array as an argument to a function, you're passing a pointerto the array. If you want to pass the size of the array, you should pass it as a separated argument.
请记住,在 C 中,当您将数组作为参数传递给函数时,您传递的是指向该数组的指针。如果你想传递数组的大小,你应该将它作为一个单独的参数传递。
The size of a pointer and an int
is 4 or 8 or something else - depending on ABI
.
In your case, it's 4
, so you're getting sizeof(int *)/sizeof int
which is 1.
指针和 an 的大小int
为 4 或 8 或其他值 - 取决于ABI
.
在您的情况下,它是4
,所以您得到的sizeof(int *)/sizeof int
是 1。
Here is a useful trick
这是一个有用的技巧
You can store the length of the array in the first element of it:
您可以将数组的长度存储在它的第一个元素中:
int myArray[]= {-1, 1, 2, 3, 4, 5};
myArray[0] = sizeof(myArray) / sizeof(myArray[0]) - 1;
//The -1 because.. the first element is only to indicate the size
Now, myArray[0]
will contain the size of the array.
现在,myArray[0]
将包含数组的大小。
回答by Grijesh Chauhan
In function decalration, array
is a pointer:
在函数声明中,array
是一个指针:
int fnLenghthOfArray(int arry[])
^
is same as int* array
And in your system sizeof(int*) == sizeof(int)
.
并在您的系统中sizeof(int*) == sizeof(int)
。
回答by ogni42
You function declaration
你的函数声明
int fnLenghthOfArray(int arry[]);
is equivalent to
相当于
int fnLenghthOfArray(int* arry);
hence your calculation yields 1 (based on the assumption that the size of a pointer to int and size of an int are the same).
因此,您的计算结果为 1(基于指向 int 的指针大小和 int 大小相同的假设)。
Your only option to get the size of the array is to provide an additional parameter
获取数组大小的唯一选择是提供一个附加参数
int fnLenghthOfArray(int arry[], std::size_t size);
Alternatively you could use one of the C++ containers like vector
or array
或者,您可以使用 C++ 容器之一,例如vector
或array
回答by Sebastian-Lauren?iu Plesciuc
int arry[]
is equivalent to
相当于
int *arry
and the sizeof()operator returns 4 when applied to arrybecause it's the size of a pointer (or reference in the case of arry[]), the size of the int is also 4 bytes and that's why it always returns 1.
并且sizeof()运算符在应用于arry时返回 4,因为它是指针的大小(或在 arry[] 的情况下是引用),int 的大小也是 4 个字节,这就是它总是返回 1 的原因。
To solve your problem you must implement the array in a different way. Maybe the first element should always have the size of the array. Otherwise you could use the vectorclass from STL or list.
要解决您的问题,您必须以不同的方式实现数组。也许第一个元素应该始终具有数组的大小。否则,您可以使用STL 或list 中的vector类。
回答by Giuseppe Pes
int fnLenghthOfArray(int arry[]){
return sizeof(arry)/sizeof(int); // This always returns 1
}
This function returns 1 because is performing a division between the size of a pointer and the size of an integer. In most architectures, the size of a pointer is equal to the size of an integer. For instance, in the x86
architecture both have size 4 bytes.
此函数返回 1,因为它正在执行指针大小和整数大小之间的除法。在大多数体系结构中,指针的大小等于整数的大小。例如,在x86
体系结构中,两者的大小都是 4 个字节。
Where as, sizeof(arry)/sizeof(int) gives the actual length of the array, in the function where it is declared
其中, sizeof(arry)/sizeof(int) 给出数组的实际长度,在声明它的函数中
Because in this case the compiler knows that arry
is an array and its size. Whereas, in the previous function, the compiler knows arry
only as a pointer. In fact, when you specify the function prototype, there is not difference between int arry[]
and int * arry
.
因为在这种情况下,编译器知道这arry
是一个数组及其大小。而在前面的函数中,编译器arry
只知道一个指针。事实上,当你指定函数原型时,int arry[]
和之间没有区别int * arry
。
回答by Ari
You can't get size of array in C
or C++
.
您无法在C
or 中获取数组的大小C++
。
Array in this languages is simply pointer to first element. You need to keep size of array by yourself.
这种语言中的数组只是指向第一个元素的指针。您需要自己保持数组的大小。
回答by HaSeeB MiR
Here is code snippet using Maroun's trick.
这是使用 Maroun 技巧的代码片段。
#include<stdio.h>
void print_array(int *array);
void shift_array_normal(int *array,int arrayLen);
int main(void)
{
int array[]= {-1,32,44,185,28,256,22,50};
array[0] = sizeof(array) / sizeof(array[0]) - 1;
print_array(array);
return 0;
}
void print_array(int *array){
int index,arrayLen = array[0];
//length of array is stored in arrayLen now we can convert array back.
printf("Length of array is : %d\n",arrayLen);
//convert array back to normal.
shift_array_normal(array,arrayLen);
//print int array .
for(index = 0; index < arrayLen; index++)
printf("array[%d] = %d\n",index,array[index]);
}
/*removing length element from array and converting it back to normal array*/
void shift_array_normal(int *array,int arrayLen){
int index;
for(index = 0; index < arrayLen; index++)
array[index] = array[index + 1];
}
回答by Titanium Mesh
#include<iostream>
int main()
{
int array[300];
int d = sizeof(array)/4;
std::cout<<d;
}
Use:
用:
// sizeof(array)/4 for "int" array reserves 4 bits.
// sizeof(array)/4 for "float" array reserves 4 bits.
// sizeof(array) for "char" array reserves 2 bits.
// sizeof(array) for "bool" array reserves 2 bits.
// sizeof(array)/8 for "double" array reserves 8 bits.
// sizeof(array)/16 for "long double" array reserves 16 bits.