1
I need to pass a float vector for a function that dynamically allocates memory and then returns this inverted vector. However, my code tells me that I have an error here:
program. c: In Function 'Reverse':
script. c:13:9: error: incompatible types when returning type 'float *' but 'float' was expected new Return;
I understand what the error is, but I can’t figure out how to allocate memory and turn that vector into a float. When I make the allocation the program returns me a pointer to float.
How I’m gonna make this conversion ?
#include <stdio.h>
#include <stdlib.h>
float reverse(float* v, int n ) {
float *novo;
novo = (float *) malloc (n * sizeof(float));
if (novo == NULL) {printf("Falta memoria\n"); exit(1);}
for(int i = 0,j = n -i -1 ; i < j ;i++, j--) {
int tmp = v[i];
v[i] = novo[j];
novo[j] = tmp;
}
return novo;
}
void printFloatArray(float *v, int n) {
for (int i = 0; i < n; i++) {
printf("%1.6f ",v[i]);
}
}
int main(void) {
float *novo;
float v[10] = {1,2,3,4,5,6,7,8,9,10};
printf("Vetor original:\n");
printFloatArray(v,10);
printf("\n");
printf("Vetor invertido:\n");
reverse(v,10);
printFloatArray(novo,10);
printf("\n");
system("pause");
return 0;
}