function returning pointer
Code:
#include<stdio.h>
int* func(int x);
int c[5]={9,6,7,8};
int main()
{
int a[5]={1,2,3,4};
int *p;
p = func(a[0]);
printf("%d",p[1]);
}
int* func(int x)
{
int* b = new int[5];
if((x==c[0]||x==c[1]))
{b[0]=10;}
else
{
b[0]=25;
b[1]=20;
b[2]=69;
}
return b;
delete b; // can i place delete here ?
}
question
------------
this code is working. but how do i know how many elements are actually returned by the ponter b ?
in fact here 3 elements(i.e b[0],b[1],b[2]) are returned in the above code.
how does the caller know how many is returned so that he could print all the returned elements.