user2296424
user2296424

Reputation:

Returning an integer array pointer in C

I am trying to return a pointer to an array of integers representing the result of applying each function in the array to value n.

#include <math.h>
#include <stdio.h>

typedef int (*funT)(int);
int *mapApply(int n, funT fs[], int size);
int func1(int);
int func2(int);
int func3(int);
int func4(int);
int func5(int);

int main() {

    funT array[5] = { func1, func2, func3, func4, func5 };
    mapApply(2, array, 5 );

    return 0;
}

int func1 (int n) {
    return n + 1;
}

int func2 (int n) {
    return n + 2;
}

int func3 (int n) {
    return n + 3;
}

int func4 (int n) {
    return n + 4;
}

int func5 (int n) {
    return n + 5;
}

int *mapApply(int n, funT fs[], int size) {
    int result[size] = { fs[0](n), fs[1](n), fs[2](n), fs[3](n), fs[4](n) };
    return &result;
}

Currently my mapApply function isn't working. Here is the compilation error:

prog.c: In function ‘mapApply’: prog.c:41:2: error: variable-sized object may not be initialized int result[size] = { fs0, fs1, fs2, fs3, fs4 }; ^ prog.c:41:2: warning: excess elements in array initializer [enabled by default] prog.c:41:2: warning: (near initialization for ‘result’) [enabled by default] prog.c:41:2: warning: excess elements in array initializer [enabled by default] prog.c:41:2: warning: (near initialization for ‘result’) [enabled by default] prog.c:41:2: warning: excess elements in array initializer [enabled by default] prog.c:41:2: warning: (near initialization for ‘result’) [enabled by default] prog.c:41:2: warning: excess elements in array initializer [enabled by default] prog.c:41:2: warning: (near initialization for ‘result’) [enabled by default] prog.c:41:2: warning: excess elements in array initializer [enabled by default] prog.c:41:2: warning: (near initialization for ‘result’) [enabled by default] prog.c:42:2: warning: return from incompatible pointer type [enabled by default] return &result; ^ prog.c:42:2: warning: function returns address of local variable [-Wreturn-local-addr]

Upvotes: 1

Views: 385

Answers (2)

alk
alk

Reputation: 70883

Variable length arrays cannot be initialised using an initialiser list.

Doing this

size_t s = 4;
int a[s] = {1, 2, 3, 4};

is not valid C.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

When you do this

int *mapApply(int n, funT fs[], int size) {
    int result[size] = { fs[0](n), fs[1](n), fs[2](n), fs[3](n), fs[4](n) };
    return &result;
}

you have two errors:

  • A pointer to result is incompatible to the return type of mapApply, and
  • You are trying to return a pointer to a local array.

To fix this, you need to allocate the array dynamically, or pass a buffer into the function. Here is how you allocate the array dynamically:

int *mapApply(int n, funT fs[], int size) {
    int *result = malloc(sizeof(int)*size);
    for (int i = 0 ; i != 5 ; i++) {
        result[i] = fs[i](n);
    }
    return result;
}

The caller needs to free the result of the call of manApply in order to avoid a memory leak.

Upvotes: 4

Related Questions