1

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

8
  • why do you need values[numOfValues] to be static? Commented Aug 6, 2019 at 3:28
  • 1
    C11 Standard - 5.1.2 Execution environments "All objects with static storage duration shall be initialized (set to their initial values) before program startup." (that cannot be satisfied with a VLA) See C11 Standard - 6.2.4 Storage durations of objects(p3) Commented Aug 6, 2019 at 3:31
  • @CoffeeTableEspresso I thought I should use static because otherwise I would be passing the address of a local variable to the main which is not usually considered proper. Commented Aug 6, 2019 at 3:32
  • 1
    Omid my answer should work for you, as @DavidC.Rankin mentioned, you can't have a static VLA. Commented Aug 6, 2019 at 3:34
  • 1
    returning that is Commented Aug 6, 2019 at 3:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

12 Comments

This code still does not compile and gives these errors: C2057: expected constant expressio, C2133: 'valuesArray' : unknown size, C2466: cannot allocate an array of constant size 0
I literally just compiled it on my machine
@Omid You have a substandard C compiler (MSVC) which does not support variable-length arrays. You need to use malloc in main too
@AnttiHaapala Thanks but I have no idea how to use malloc yet. Need to learn first and it seems very complicated to me.
@Omid you can't really do this without malloc especially if your compiler doesn't support VLAs properly.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.