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.
Related
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.
}
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:
C++ sizeof Vector is 24?
(1 answer)
sizeof() a vector
(6 answers)
Closed 3 years ago.
when I try to take the element by sizeof , then I am getting different answers in array and vector
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> num = {1,2,5,4};
int k = sizeof(num)/sizeof(num[0]);
cout << k << endl;
int nums[] = {1,2,5,4};
int w = sizeof(nums)/sizeof(nums[0]);
cout << w << endl;
}
Expected 4 4
Output 3 4
Let's see what sizeof returns:
sizeof Queries size of the object or type.
When applied to a class type, the result is the size of an object of
that class plus any additional padding required to place such object
in an array.
vector<int> and int [] are totally different things. Vector uses dynamic storage while array uses automatic storage.
sizeof(vector) return the size of vector class. It doesn't work the way you expect for the same reason it won't work on a dynamiclly allocated array.
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 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