Hi, I'd like to get the size of the pointer array passed to the function.
(On line 20)
arrSize=sizeof(inputArr)/sizeof(inputArr[0]) ;
did not work.
On line 20, if I use
arrSize=std::array::size(inputArr);
it will say
"print n of n back\main.cpp|20|error: 'template<class _Tp, unsigned int _Nm> struct std::array' used without template parameters|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|"
"
print n of n back\main.cpp|19|error: request for member 'size' in 'inputArr', which is of non-class type 'node**'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| "
#include <array>
...
template <class T, size_t N> class array;
...
int arrSize=std::array::size(inputArr);
Are you re-defining std::array? What are you trying to achieve? You are mixing C and C++ headers and programming styles together. Could you please describe what your code should do?
I want to create an array of pointers, and find its size in a function without passing its size to the function (only pass the array of pointers to the function)
#include <array>
struct node*{
int value;
node* next;
}
void someFunc (node* arr[], int irrelevant) {
int arrSize= ... // I need to find the size of the array here
}
int inputArrSize, someNumber=0;
int main() {
cin >> inputArrSize;
...
node (*arr)[inputArrSize];
...
someFunc (arr,someNumber)
return 0;
}
I want to create an array of pointers, and find its size in a function without passing its size to the function (only pass the array of pointers to the function
size of any array (both T[N] and array<T,N>) is known at compile time, so your function has to be a template in order to be callable with arrays of different size
This function does not take an array. It's the old C bug/feature/hack for backwards compatibility with B that arrays cannot be used as function parameters, but instead of making that an error, the C compiler quietly replaces that function declaration with one taking a pointer - the function you declared is really void someFunc (node** arr, int irrelevant), arrays are not involved in any way.
1 2
cin >> inputArrSize;
node (*arr)[inputArrSize];
Even if you fix that to create an array of pointers (currently it creates a pointer to an array) this is invalid: inputArrSize is not a constant and you cannot declare an array of non-constant size in C++. If you're using a compiler that allows that, fix it by using commandline option -pedantic-errors
If your size is not known until runtime, use std::vector<node*>