Error in C program compatibility between float and float pointer

Asked

Viewed 79 times

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;
}

2 answers

1


First, the declaration of your role reverse should be:

float* reverse(float* v, int n)

Because in it you intend to return something of the pointer type to float, which will indicate the memory address of your new vector.

Then in the function main, you should update the variable novo to receive the value returned by the function reverse, so:

novo = reverse(v,10);

Note that without this you will not be changing its value anywhere in the function main. The variable novo who is in the function reverse is another because it is in a scope different. You need to make the assignment with the value returned to effect this call.

Finally, the reversal logic within the for is incorrect. If you use one iterator from beginning to end and another from end to beginning, you can use them to index and do the correct assignment at each position of the new vector with the respective value of the old vector.

0

I believe you are in doubt as to the return of your function float reverse(float* v, int n ), that could be so: float *reverse(float* v, int n ) {. But privately I would do so:

#include <stdio.h>
#include <stdlib.h>

float *allocFloatArray(int numberOfPositions){

    float *pointer;

    pointer = (float *) malloc (numberOfPositions * sizeof(float));

    if (pointer == NULL) { printf("Falta memoria\n");  exit(1); }

    return pointer;
}

void reverse(float* v, float* novo, int n) {

    for(int i = 0, j = n - 1; 
        i < n;
        i++, j--) {

        novo[j] = v[i];  
    }
}

void printFloatArray(float *v, int n) {
    for (int i = 0; i < n; i++) {
        printf("%1.6f  ",v[i]);
    }
}


int main(void) {

    float *novo;

    novo = allocFloatArray(10);

    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, novo, 10);

    printFloatArray(novo,10);

    printf("\n");

    return 0;
}

This way, I allot a float array of the size you want, assign the memory address of this vector to my variable in the main function (novo = allocFloatArray(10);) and I use another function to invert the values. Leaving each function with a well-defined responsibility.

I hope I helped. Anything, we’re there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.