A
Andrew Gentile
Hello,
I have been working on a program where I need to have a function
return an array. I found out that C doesn't do this, so now I am
trying to get the function to return a pointer to an array. It seems
easy, but I keep running into the same problem. The function appears
to execute properly, but when I print out the array values, only the
first one is correct. The other are all wrong. A snippet of code is
below. Please take a look and see what I have done wrong. Thank you
in advance for your help.
void main(void)
int *point(int); // function proto
int *p, i,count;
i=0;
count=5;
p=point(count); // call function, p should be value at array x2[0]
for(i=0;i<count;i++) // print out array
{
printf("\nfunction retun = %i",*p);
p++;
}
}
int *point(int k) // function to create array k-1 integers long.
{
int *p2,x2[k],i2;
i2=0;
while(i2<k)
{
x2[i2]=3*k;
i2++;
}
p2=x2;
return p2;
}
I have been working on a program where I need to have a function
return an array. I found out that C doesn't do this, so now I am
trying to get the function to return a pointer to an array. It seems
easy, but I keep running into the same problem. The function appears
to execute properly, but when I print out the array values, only the
first one is correct. The other are all wrong. A snippet of code is
below. Please take a look and see what I have done wrong. Thank you
in advance for your help.
void main(void)
int *point(int); // function proto
int *p, i,count;
i=0;
count=5;
p=point(count); // call function, p should be value at array x2[0]
for(i=0;i<count;i++) // print out array
{
printf("\nfunction retun = %i",*p);
p++;
}
}
int *point(int k) // function to create array k-1 integers long.
{
int *p2,x2[k],i2;
i2=0;
while(i2<k)
{
x2[i2]=3*k;
i2++;
}
p2=x2;
return p2;
}