Search code examples
cfunctionpointersreturn-type

correct return type for C function?


I am using an array of strings inside my function declared like this

char (*array)[PATH_MAX] = malloc (1000 * sizeof *array);

Allocating 1000 char pointers of size PATH_MAX (1024).

I want the function to return that specific data structure, i.e. return the pointer array. But I can't manage to choose the correct return type.

warning: returning 'char (*)[1024]' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion]

I get that I can't have int as return type, but char** doesn't work either.

warning: returning 'char (*)[1024]' from a function with incompatible return type 'char **' [-Wincompatible-pointer-types]

I've tried using char (*)[1024] as return type, but it gives me generic C error messages, leading me to believe that I am not using the correct syntax.

error: expected identifier or '(' before ')' token
   38 | char (*)[1024] read_dir (char *path) {
      |        ^

Is there a correct syntax for achieving this or am I just doing it wrong?


Solution

  • Time for typedef

    typedef char arr1k_char[1000];
    
    arr1k_char *foo(int n) {
        arr1k_char *x = malloc(n * sizeof *x);
        return x;
    }
    

    https://ideone.com/Cqxa61