user2737926
user2737926

Reputation: 97

Compile time warnings double pointer in C language

Here, I have created a structure called directory. Which has double pointer to children and single pointer to parent.

    typedef struct {
        struct directory** children;
        struct directory* parent ; 
    }directory;

Here, I have created a function to add directory.

    directory* add_dir (char* name , char* path , directory* parent) {

        directory* d = malloc (sizeof (directory)) ;
        memset ( d , 0 , sizeof(directory)) ;
        //WARNING by line below
        d->children = (directory**)malloc ( sizeof(directory*) * DIR_COUNT) ;
        memset ( d->children , 0 , sizeof(directory*) * DIR_COUNT) ;
        //WARNING by line below
        d->parent  = (directory*) parent ;
            //Wanrning by line below
            parent->children[parent->alloc_num_child] = (directory*) d ;
    }

I have defined a struct called directory which has double pointer to children and single pointer to parent directory. When I compile this I am getting warnings.

Warnings :


warning: assignment from incompatible pointer type [enabled by default]
warning: assignment from incompatible pointer type [enabled by default]
warning: assignment from incompatible pointer type [enabled by default]

I am not sure why am I getting this warnings ?

Upvotes: 0

Views: 139

Answers (1)

gnasher729
gnasher729

Reputation: 52538

Look exactly at your struct declaration.

You declare a typedef named directory. You do not anywhere declare a struct directory. But you are using "struct directory" inside the untagged struct that you declare.

The compiler has no way to guess that you mean the typedef named "directory" when you write "struct directory".

I usually would write

typedef struct _directory {
    struct _directory** children;
    struct _directory* parent ; 
}directory;

Upvotes: 2

Related Questions