rita
rita

Reputation: 111

How to return an array in C?

i am trying to return an array from this code, so that i can later create an array of arrays, in another method i'm writing...

int getValues(char *userInputPtr) {

    int i = 1;
    float fp = 0;
    float num = atof(userInputPtr);


    float *arrayOfFloats = (float *)malloc(sizeof(float) * num);

    for ( i ; i <= num ; i++ ) {
        scanf(" %f", &fp);
        arrayOfFloats[i] = fp;
        printf(" %f", arrayOfFloats[i]);

    }
    printf("\n");

    return arrayOfFloats;

 }

i keep getting the error: warning: return makes integer from pointer without a cast. i just want to return the array! what gives? :/

Upvotes: 0

Views: 155

Answers (1)

ouah
ouah

Reputation: 145919

Change the return type of your function from int to the correct type you are returning, i. e., change:

int getValues(char *userInputPtr)

to

float *getValues(char *userInputPtr)

Of course, this will not technically return an array (C does not allow arrays to be returned), but a pointer to the first element of the C array.

Upvotes: 1

Related Questions