2

I am having trouble understanding why the compiler is giving me the following error:

level0.c: In function ‘create_grid’: level0.c:28:9: warning: return from incompatible pointer type [-Wincompatible-pointer-types] return grid;

I am trying to return a pointer to a struct that I created of type struct gridType in the function. That is also the type that the function expects to be returned.

The code for the function is:

struct gridType* create_grid(int length){

    char** array = malloc(length * sizeof(*array));
    for(int i = 0; i < length; i++){
        array[i] = malloc(length * sizeof(array));
    }   

    for(int i = 0; i < length; i++){
        for (int j = 0; j < length; j++){
            array[i][j] = '-';
        }   
    }   

    struct gridType{
        int length; 
        char** array;
    };

    struct gridType* grid = malloc(sizeof(struct gridType));

    grid->length = length;
    grid->array = array;

    return grid;
}

1 Answer 1

2

You can't define struct gridType inside your function and expect to be able to return it (for other people to see).

Type moving

struct gridType{
    int length; 
    char** array;
};

Outside (before) the function create_grid().

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.