Thread: dereferencing pointer to incomplete type

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    2

    dereferencing pointer to incomplete type

    so i need to create a function that creates a 2 dimensional field of pointers to a structure called a "cell".

    this is the code for the structure, nothing too hard here

    Code:
    #ifndef CELL_H_
    #define CELL_H_
    
    /*
     * Dit zijn de twee groepen die er bestaan in de variant op Conway's Game of Life die jullie moeten implementeren.
     * Een cel behoort altijd tot een van deze twee groepen.
     */
    enum Group {
        A, B
    };
    
    /*
     * Dit zijn de mogelijke staten waarin elke cel zich bevindt: een cel is altijd ofwel dood ofwel levend.
     */
    enum State {
        ALIVE, DEAD
    };
    
    /*
     * Een cel wordt voorgesteld als een struct die tot een bepaalde groep hoort (groep A of groep B)
     * en een bepaalde staat heeft (levend of dood).
     */
    struct Cell {
        enum Group group;
        enum State state;
    };
    
    
    #endif /* CELL_H_ */
    and here is the code for the function that allocates a pointer to a "cell" structure and where every cell is initialized to the state "DEAD". (its an implementation of conways game of life). the error appears on line 26 which is

    Code:
    cell_pointer->state = 1;
    full code :
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    struct Cell ***allocate_field(int field_width, int field_height){
        //creer geheugen voor het aanmaken van de nodige vectoren van pointers
        struct Cell*** kolommen = malloc(field_width * sizeof *kolommen);
    
        int i;
        int j;
    
        for(i=1;i == field_width;i++){
            kolommen[i] = malloc(field_width * sizeof (struct Cell**));
            for(j=1;j == field_height;j++){
                struct Cell *cell_pointer;
                kolommen[i][j] = malloc( sizeof (struct Cell*));
                kolommen[i][j] = cell_pointer;
                cell_pointer->state = 1;
            }
        }
        return kolommen;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh:
    Quote Originally Posted by jeltedeproft
    Code:
    struct Cell ***allocate_field(int field_width, int field_height){
    While it is not a hard and fast rule, generally having so many levels of indirection is to be avoided. Programmers who do this too often may end up with the sarcastic title of Three Star Programmer.

    I suggest doing a little more abstraction, e.g.,
    Code:
    struct Field {
        struct Cell **cells;
    };
    This way, you can write:
    Code:
    struct Field *allocate_field(int field_width, int field_height) {
        struct Field *field = malloc(sizeof(*field));
        field->cells = malloc(field_height * sizeof(field->cells[0]));
    
        int i;
        int j;
    
        for (i = 0; i < field_height; i++) {
            field->cells[i] = malloc(field_width * sizeof(field->cells[i][0]));
            for (j = 0; j < field_width; j++) {
                field->cells[i][j] = DEAD;
            }
        }
        return field;
    }
    Notice a few things:
    • With the fewer levels of indirection, it was easier to see that you want one malloc for the field itself, one more for the outer dynamic array, and one more per each element of the outer dynamic array to create the inner dynamic arrays.
    • I switched the height and width to make it row-major, as in normal C 2D arrays.
    • The array indices start from 0, so I did that instead of starting from 1.
    • You did well to do sizeof *kolommen, but became inconsistent later. I consistently made use of the destination for sizeof.
    • Instead of using the magic number 1, I used the named constant DEAD.

    Of course, this will not solve your program, which lies here:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    You forgot to #include "cell.h", or whatever your header file is named.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    2
    thanks a lot, the issue was indeed that i forgot to include allocate_field.h, its always something dumb like this

    the reason i did not notice this, was because eclipse recognized the cell structure, therefore i thought that the include files couldn't be the problem.

    Thanks for cleaning up my code, but i cannot use your version, because this is a task for school and they already gave us the declaration of the function , so we have to use the triple pointer style

    many thanks, finally i can move on , on my project

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dereferencing pointer to incomplete type
    By killermelga in forum C Programming
    Replies: 4
    Last Post: 11-09-2011, 03:20 PM
  2. Another 'dereferencing pointer to incomplete type'
    By thealmightyone in forum C Programming
    Replies: 7
    Last Post: 11-25-2010, 08:49 PM
  3. dereferencing pointer to incomplete type
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 07-28-2010, 09:32 AM
  4. dereferencing pointer to incomplete type
    By johnny_ in forum C Programming
    Replies: 4
    Last Post: 03-01-2010, 10:46 AM
  5. dereferencing pointer to incomplete type
    By kataya in forum C Programming
    Replies: 2
    Last Post: 04-16-2008, 01:37 AM

Tags for this Thread