I'm creating an integer array whose input will be given by user, and I'm passing it to a function where it is decaying to a pointer(say int * a), now how can I obtain the length of the array in my function using only the available resource, that is the pointer to the first array element?
I have tried "templates"(template) which works on array and not on pointer. I did not want to use "struct"(putting array inside struct and passing the struct to function) as it just avoids the problem. I also did not want to stash the size in one block extra in the array as I did not want to consume more memory than what is required(even though it is a mere one). I also did not want iterators("begin" and "end"), they mostly do not work on pointers. I did not want a C++11 solution or a boost library solution too as I did not want to depend on version or a library.
#include <iostream>
using namespace std;
void func(int *a)
{
//Now entire programming is to be done here as array has been passed
//How to find length using pointer a only, so that I can proceed with
//array operations?
//This function is not aware of length because of Array Decay but
//atleast has a starting point that is the pointer.
}
int main()
{
int array[5]={2,3,4,5,6};
func(array);
//main()'s use is over now, control is shifted to void func(int *)
return 0;
}
I expect a method to find length using the pointer only, that is I expect the number 5(length of array) to get generated somehow inside func() method.