Why can you pass an array as a pointer? [duplicate] - c++

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]));
}

Related

How to calculate the length of a string array in C++ [duplicate]

This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
What is array to pointer decay?
(11 answers)
Closed 3 months ago.
I know how to calculate the length for a string array in c++ (sizeof(arr)/sizeof(arr[0])), but I'm having a problem calculating the length of a string array if I pass an array through a method that's inside another class. Is there a reason why this happens?.
main.cpp
/* Allways returns 3 */
std::string arr[3];
int length = sizeof(arr)/sizeof(arr[0]);
std::cout << length << std::endl;
main.cpp but calling the method from a class
/* Allways returns 0 */
Test* test = new Test();
std::string arr[3];
test->length(arr);
Test.cpp
void Test::length(std::string arr[]){
int length = sizeof(arr)/sizeof(arr[0]);
std::cout << length << std::endl;
}
I have passed in the array as a reference but It's not letting me do that, I have tried converting the array into a char array and passing in the value.

Is there any way to convert a array pointer back into a regular array? [duplicate]

This question already has answers here:
Finding length of array inside a function [duplicate]
(7 answers)
size of array passed to C++ function? [duplicate]
(7 answers)
Closed last year.
I am trying to pass an array through a function but when I try to get the length of the array it gives me the length of the pointer. Is there any way to convert the array pointer back into a regular array?
float arr[] = {10, 9, 8]
void func(float arr[])
{
// now I want to figure out the size of the array
int lenArr = sizeof(arr) / sizeof(arr[0]); // this will get the size of the pointer to the array and not the actual array's size
}
You can declare the parameter a reference to an array.
void func(float (&arr)[10])
{
// but you have to know the size of the array.
}
To get around having to know the size, you can template on size
template<int Size>
void func(float (&arr)[Size])
{
// Now the size of the array is in "Size"
// So you don't need to calcualte it.
}

Sizeof in function [duplicate]

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();
}

c++ size of array is changing using sizeof [duplicate]

This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 8 years ago.
i have simple main that called simple methods with array as parameter
the size in the array is right , but then when i try to print the array im getting different
sizeof array :
int bubbleSort(int arr[]) // yeah i know this sort is not complete
{
int arrSize = sizeof(arr); // HERE IS SIZE IS 4
bool bSorted = true;
while(bSorted)
{
for(int i=0;i<arrSize;i++)
{
if(arr[i]>arr[i+1])
{
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
}
}
bSorted = false;
}
return 1;
}
int main(int argc, char* argv[])
{
int arr[] = {4,3,7,8,9};
bubbleSort(arr);
int sOf = sizeof(arr); // HERE THE SIZE IS 20 ????
for(int j=0;j < sOf ;j++)
{
printf("%d",arr[j]);
}
return 0;
}
You cannot pass arrays* by value as function parameters (nor return them in such fashion). The syntax void f(int a[]) is merely syntactic sugar that is in every aspect identical to void f(int * a), and the function parameter is a pointer, not an array. So you end up measuring the size of the pointer.
The alternative syntax may serve as a loose kind of documentation of intent, signalling that you plan to call this function with the address of an array element, but there is no actual difference to the pointer syntax.
*) Array types and function types are both constrained thus, which is why people often say that they are not first-class citizens of the type system.
Due to the language being stupid, this:
int bubbleSort(int arr[])
actually means this:
int bubbleSort(int* arr)
So sizeof(arr) is the size of a pointer.

Passing an array of ints as an argument in C++? [duplicate]

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;
}