The number of elements used could either be passed as a parameter to the function (in the form of a pointer, of course), or you could use a sentinel value to mark the end of the array (just as strings are zero-terminated).
>> return b;
>> delete b;
Anything that appears after a return statement will not get executed. Second, you shouldn't delete the memory from within the function since it is being used afterward! Instead, either return the dynamic memory to the caller, and let the caller clean it up later (not recommended), or just create the array from the callers scope and pass it as a parameter to the function.
Another thing I noticed was that the function references a global variable. This is almost always a very bad idea. A much better way would be to pass the variable as a parameter.
Code:
#include<stdio.h>
int * func(
int param,
int dest[],
int src[],
int max_elements,
int * used);
int c[5]={9,6,7,8};
int main()
{
int a[5]={1,2,3,4};
int b[5];
int *p;
int total;
p = func(a[0], b, c, 5, &total);
printf("%d",p[1]);
return 0;
}
int * func(
int param,
int dest[],
int src[],
int max_elements,
int * used)
{
*used = 0;
if((param==src[0]||param==src[1]))
{
if(max_elements >= 1)
{
dest[0]=10;
*used = 1;
}
}
else
{
if(max_elements >= 3)
{
dest[0]=25;
dest[1]=20;
dest[2]=69;
*used = 3;
}
}
return dest;
}