Let’s say we havew a function that returns an array.
int *theFunction()
{
int a[]={1,2,3,4,5};
return a;
}
I want to store the result of the function in a pointer.
int *a=theFunction();
How do I print the array, is it even possible?
The length of the array isn’t known, is there a way to find it?
You cant print an
arraywhen itssizeis unknownYou can get around this problem by
1>providing a sentinal value i.e the value that would denote the end of the array.
Let us consider
-1to be the sentinal thenBut this solution would fail if one of your value seems to be
-1OR
2>Use a struct to denote the size and the array content
Also dont return a pointer to value that has local scope..i.e in your case the local value is
a..As soon as you get out of the method a is destroyed..further access to thisaafter the method returns is an error