Reputation: 27
The output shows the values of k
as 0.000
but it should contain actual values that are returned from funcTest()
.
#include <stdio.h>
#include <stdlib.h>
float *funcTest(int *a, int size)
{
float p[size];
int i;
for(i=0; i< size; i++){
p[i] = *a;
p[i]=p[i]/2;
a++;
}
for(i =0; i<size; i++){
printf("%f\n",p[i]);
}
return (float *)p;
}
int main()
{
int a[4] = {1,5,3,7};
int size = 4;
int i,j;
float *k;
k = funcTest(&a[0],size);
for(i =0; i<size; i++){
printf("%f\n",*k);
k++;
}
return 0;
}
OUTPUT :
0.5
2.5
1.5
3.5
1.5
0.000
0.000
0.000
Upvotes: 2
Views: 12235
Reputation: 1
#include <stdio.h>
#include <stdlib.h>
void funcTest(int *a, int size, float *output)
{
output = (float *) malloc(sizeof(float) * size);
int i;
for(i=0; i< size; i++){
output[i] = *a;
output[i]=output[i]/2;
a++;
}
for(i =0; i<size; i++){
printf("%f\n",output[i]);
}
}
int main()
{
int a[4] = {1,5,3,7};
int size = 4;
int i,j;
float *k;
funcTest(&a[0],size, k);
for(i =0; i<size; i++){
printf("%f\n",k);
k++;
}
return 0;
}
This might be worked.
Upvotes: 0
Reputation: 106092
p
is an automatic local variable and will no longer be exist once function return. Returning a pointer to it will invoke undefined behavior.
Allocate p
dynamically
float *p = malloc(size*sizeof(float));
Free the allocated memory once you are done with it
float *k;
k = funcTest(&a[0],size);
if(k == NULL) // Check for null pointer
exit(0);
float *temp = k;
for(i =0; i<size; i++){
printf("%f\n",*k);
k++;
}
free(temp); // Free allocated memory
return 0;
Upvotes: 2
Reputation: 3707
You return a local variable. so, at end of function, local variables are cleaned and do not exist any more. You should use malloc
float *funcTest(int *a, int size)
{
float *p = malloc(sizeof(float) * size);
And don't forget to free memory when you don't use it anymore with free
. If you forget, you will have a memory leak
Upvotes: 3
Reputation: 19221
As the people before me pointed out, you're trying to return a stack allocated variable (a local variable in a function), meaning the memory you point to isn't valid once the function exits (it clears out on some implementations, but really it's behavior isn't defined).
The solution is to either use malloc
, as pointed out before, OR pass a pointer to the function (thus avoiding malloc
and free
), keeping the allocation on the stack of the calling function.
i.e. (this might not work, it isn't tested, just here for demonstration of the idea):
#include <stdio.h>
#include <stdlib.h>
int funcTest(float *p, int *a, int size)
{
int i;
for(i=0; i< size; i++){
p[i] = *a;
p[i]=p[i]/2;
a++;
}
for(i =0; i<size; i++){
printf("%f\n",p[i]);
}
// we can use the return value for errors.
return 0;
}
int main()
{
int a[4] = {1,5,3,7};
float p[4] = {0};
int size = 4;
int i;
funcTest(p, a, size);
for(i =0; i<size; i++){
printf("%f\n",p[i]);
}
return 0;
}
Notice also, you don't need to use &a[0]
- arrays are pointers at heart, so a
is the memory address of a[0]
while a + 1
is the memory address of a[1]
.
Good Luck!
Upvotes: 2
Reputation: 134356
In your code, float p[size];
is local to the function funcTest()
, so you cannot return the address of it from the function and expect it to be valid in the caller. Once the funcTest()
function ends, p
will cease to exist. Then, any attempt to make use of the return value will cause undefined behavior, because of invalid memory access.
Instead, you can make p
a pointer, allocate memory dynamically using malloc()
or family and then, return that pointer to the caller. Dynamically allocated memory will have a lifetime equal to the entire execution of the program (unless deallocated manually), so, even after returning from the function, in the caller, the returned pointer will be valid.
Upvotes: 2