typedef struct bunny_info{

    /* MORE MEMBERS HERE */

    struct bunny_info *nextBunny;
}bunny;

bunny *make_bunny(){
    bunny *bunnyPtr;

    /* SOME CODE HERE */

    return bunnyPtr;
}

bunny *makebunny(bunny *bunnyList, short int makeCount){
    bunny *newBunny = NULL, *endPtr = bunnyList;
    while(makeCount > 0){
        if(bunnyList != NULL){
            bunny *temp = bunnyList;
            while(temp->nextBunny != NULL){
                temp = temp->nextBunny;
            }
            endPtr = temp->nextBunny;
        }
        *endPtr = make_bunny(); // ERROR IS HERE, INCOMPATIBLE TYPE. COMPILE TIME.
    }
}

I don't get why I'm having a compile time error when the contents of the pointer is also a pointer of the same type. I'm getting no errors when I remove the dereferencing operator but that'll lead to a runtime error.

Also, why can't I use:

typedef struct bunny_info{

    /* MORE MEMBERS HERE */

    bunny *nextBunny;
}bunny;

Recommended Answers

All 3 Replies

>*endPtr = make_bunny();
endPtr is already a pointer to bunny, which make_bunny() returns. When you dereference endPtr, it gives you a bunny object, so you're trying to assign a pointer to a bunny object. To assign the object returned by make_bunny(), you would dereference the result of the function call:

endPtr = *make_bunny(); /* Generally not recommended */

And to assign the pointer, no dereferencing is required:

endPtr = make_bunny();

Also, why can't I use:

You can't use a type before it is completely defined. This would work, and it also illustrates why your code doesn't:

typedef struct bunny_info bunny;

struct bunny_info {
    /* More members here */
    bunny *nextBunny;
};

Well that was a careless mistake... I had a different chain of thought, I guess a pointer to pointer would be more suited.

And thanks for the second one. Makes more sense. Though why would a self referential structure without the typedef work even though the structure isn't exactly completely defined as well?

struct struct_name{
struct struct_name *ptr;
}

why would a self referential structure without the typedef work even though the structure isn't exactly completely defined as well?

A structure doesn't have to be completely defined to declare a pointer to it:

struct foo *p;

struct foo {
    int bar;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.