This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is useful about a reference-to-array parameter?
Are
void myFunction( int (&arg)[4] );
and
void myfunction(int arg[4]);
different? How are they different? What do the first do and how can I call it?
They are different. The first one takes a reference to an array of 4 ints as its argument. The second one takes a pointer to the first element of array of an unknown number of ints as its argument.
int array1[4] = {0};
int array2[20] = {0};
void myFunction1( int (&arg)[4] );
void myFunction2( int arg[4] );
myFunction1( array1 ); // ok
myFunction1( array2 ); // error, size of argument array is not 4
myFunction2( array1 ); // ok
myFunction2( array2 ); // ok
Related
This question already has answers here:
determine size of array if passed to function
(10 answers)
size of array passed to C++ function? [duplicate]
(7 answers)
Closed 3 years ago.
How do I get the size of bits of an Array from a function
int NumberOfElements(int Array[]);
int main()
{
int Array[] = { 5,5,6,5,5 };
std::cout << NumberOfElements(Array);
}
int NumberOfElements(int Array[]) {
return sizeof(Array);
}
It's returning 4.
Result should be 20.
Arrays decay into pointers when passed as arguments to functions etc.
The size 4 means that the pointer has that size. It does not tell you anything about the number of elements in the actual array.
You may want to use a std::vector<int> instead where the size is part of its interface:
#include <vector>
int main()
{
std::vector<int> Array{ 5,5,6,5,5 };
std::cout << NumberOfElements(Array);
}
int NumberOfElements(const std::vector<int>& Array) {
return Array.size();
}
This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 6 years ago.
I was reading http://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm and the first method it recommends in passing an array to a function is to pass it as a pointer:
void myFunction(int *myArray) {
.
.
.
}
Wouldn't this not work, because it's impossible to determine the length of myArray in myFunction?
The usual convention is to pass a separate parameter for the array length.
void myFunction(int* myArray, int length) {
for (int index = 0; index < length; ++index) {
// ... do something with each array item ...
}
}
void caller() {
int array[10];
// ... put some useful values in the array ...
myFunction(array, sizeof(array) / sizeof(array[0]));
}
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Passing Arrays to Function in C++
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 9 years ago.
I made a function in C++ to find the length of an array. I find the sizeof the array passed in the argument and divide it by the sizeof the variable type. This should work but it always returns 1! Am I missing something obvious? Or does this have to do with pointers and memory? This is my code:
#include <iostream>
using namespace std;
int lengthOf(int arr[]);
int main() {
int x[] = {1,2,3,0,9,8};
int lenX = lengthOf(x);
cout << lenX;
return 0;
}
int lengthOf(int arr[]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}
Output (should be 6 instead of 1):
4
1
I am fairly new so excuse me if this is a bad question.
When passing an array as parameter, it always decays into a pointer. If you want to see the size of the array, you need to pass the array by reference, e.g.:
template <int Size>
int lengthOf(int (&array)[Size]) {
return Size;
}
You should use the pointer.
(sizeof(arr)/sizeof(*arr))
Even though int arr[] looks like you are passing an array, you are actually passing a pointer. int arr[] is equivalent to int* arr when used as a function parameter, this comes from C.
In C++, if you want to pass an array, the proper way is to do it by reference:
template <int N>
int lengthOf(int (&arr)[N]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
Sizeof array passed as parameter
I have this code:
class total {
public:
void display(int []);
};
void total::display(int *arr)
{
int len = sizeof(arr)/sizeof(int);
cout << len;
}
int main(void)
{
int arr[] = { 10, 0, 0, 4, 20 };
total A;
A.display(arr);
return 0;
}
The output is 1 while I expected it to be the length of array , 5
However if I use the sizeof() statement in main() it displays 5.
So, why is the sizeof() not displaying correct value inside the member function?
sizeof of an array operand yields the size of the array but sizeof of a pointer operand yields the size of the pointer. Pointer and arrays are not the same in C/C++.
The sizeof operator returns the size of the operand. In your case sizeof(arr), the type of the operand is int*. So, the result is either 4 or 8 (depending on the platform, can be also 2 or 1). There is not way to know inside the finction the length of the passed array. Even if you write
void total::display(int arr[5])
{
}
this will not change anything because arrays are converted to pointers when they are used as params of the methods. You can still pass array of any size.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C: differences between pointer and array
Is an array in C++ a pointer? Can you clarify this?
Thanks.
No. But it can decay to a pointer whenever you need it.
void foo1(char * c) {
}
int main() {
char Foo[32];
foo1(Foo); // Foo decays to a pointer
char * s = Foo; // Foo decays to a pointer which is assigned to s
}
The array name itself without any index is a pointer.
int a[10];
printf("%d\n",*a); // will print first value
printf("%d\n",*(a+1) ); // will print second value