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.
请帮我找到这个。
8 个解决方案
#1
12
Remember, in C when you pass an array as an argument to a function, you're passing a pointer to 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.
指针和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]将包含数组的大小。
#2
3
In function decalration, array
is a pointer:
在函数decalration中,array是一个指针:
int fnLenghthOfArray(int arry[])
^
is same as int* array
And in your system sizeof(int*) == sizeof(int)
.
并在您的系统sizeof(int *)== sizeof(int)。
#3
2
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
#4
1
int arry[]
is equivalent to
相当于
int *arry
and the sizeof() operator returns 4 when applied to arry because 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.
当应用于arry时,sizeof()运算符返回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 vector class from STL or list.
要解决您的问题,您必须以不同的方式实现该数组。也许第一个元素应该总是具有数组的大小。否则,您可以使用STL或list中的vector类。
#5
1
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之间没有区别。
#6
0
You can't get size of array in C
or C++
.
你无法在C或C ++中获得数组的大小。
Array in this languages is simply pointer to first element. You need to keep size of array by yourself.
这种语言中的数组只是指向第一个元素的指针。你需要自己保持数组的大小。
#7
0
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];
}
#8
-2
#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.
#1
12
Remember, in C when you pass an array as an argument to a function, you're passing a pointer to 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.
指针和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]将包含数组的大小。
#2
3
In function decalration, array
is a pointer:
在函数decalration中,array是一个指针:
int fnLenghthOfArray(int arry[])
^
is same as int* array
And in your system sizeof(int*) == sizeof(int)
.
并在您的系统sizeof(int *)== sizeof(int)。
#3
2
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
#4
1
int arry[]
is equivalent to
相当于
int *arry
and the sizeof() operator returns 4 when applied to arry because 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.
当应用于arry时,sizeof()运算符返回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 vector class from STL or list.
要解决您的问题,您必须以不同的方式实现该数组。也许第一个元素应该总是具有数组的大小。否则,您可以使用STL或list中的vector类。
#5
1
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之间没有区别。
#6
0
You can't get size of array in C
or C++
.
你无法在C或C ++中获得数组的大小。
Array in this languages is simply pointer to first element. You need to keep size of array by yourself.
这种语言中的数组只是指向第一个元素的指针。你需要自己保持数组的大小。
#7
0
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];
}
#8
-2
#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.