Search code examples
cfunctionvisual-c++return-type

Cannot resolve the errors with returning an array using pointer from a function in C?


I am trying to receive a number of float variables through a function and then pass them to the main and store them in another array in the main. The size of the array is specified by the user, so I am using a variable-length array. I have already searched and read similar questions about returning arrays from functions, but couldn't find a solution for the errors in my code.

For your convenience, I have summarized the original code and included only the lines related to this problem.

float *receiveFloatValues(int numOfValues);

int main(void)
{
    int numberOfValues;
    float *receivedValues;

    printf("\nHow many values? ");
    scanf("%d", &numberOfValues);

    receivedValues = receiveFloatValues(numberOfValues);

    float valuesArray[numberOfValues];

    for (int counter = 0; counter < numberOfValues; counter++)
    {
        receivedValues += counter;
        valuesArray[counter] = *receivedValues;
    }

    for (int counter = 0; counter < numberOfValues; counter++)
    {
        printf("\nvaluesArray[%d]: %.2f", counter, valuesArray[counter]);
    }
    return(0);
}

float *receiveFloatValues(int numOfValues)
{
    static float values[numOfValues];
    for (int counter = 0; counter < numOfValues; counter++)
    {
        printf("\nEnter value %d: ", counter + 1);
        scanf("%.2f", &values[counter]);
    }
    return(values);
}

My expected result is to display the list of float values received from user, but I get these error messages:

C2057: expected constant expression

C2133: unknown size

C2466: Cannot allocate an array of constant size 0


Solution

  • You can't have a static VLA, just use malloc and free, as shown here:

    #include <stdio.h>
    #include <stdlib.h>
    
    float *receiveFloatValues(int numOfValues);
    
    int main(void)
    {
        int numberOfValues;
        float *receivedValues;
    
        printf("\nHow many values? ");
        scanf("%d", &numberOfValues);
    
        receivedValues = receiveFloatValues(numberOfValues);
    
        float *valuesArray = malloc(sizeof(float) * numberOfValues);
    
        for (int counter = 0; counter < numberOfValues; counter++)
        {
            valuesArray[counter] = receivedValues[counter];
        }
    
        for (int counter = 0; counter < numberOfValues; counter++)
        {
            printf("\nvaluesArray[%d]: %f", counter, valuesArray[counter]);
        }
        free(receivedValues);    // free memory here.
        free(valuesArray);
        return(0);
    }
    
    float *receiveFloatValues(int numOfValues)
    {
        float *values = malloc(sizeof(float) * numOfValues);   // allocate memory here.
        for (int counter = 0; counter < numOfValues; counter++)
        {
            printf("\nEnter value %d: ", counter + 1);
            scanf("%f", &values[counter]);
        }
        return(values);
    }