zackster7171
zackster7171

Reputation: 39

C Error: Initialization From Incompatible Pointer Type

To start off, the goal with my program is to create my own malloc using the buddy allocation scheme. However, when I go to compile my code, I get the initialization from incompatible pointer type error. I am confused on what exactly this means and how I can fix it. Here is a short snippet of the function where this error is occurring:

node *fList[26] = {NULL};

void *divider(int index, int baseCase) {

    node *temporary = fList[index + 1]; 

    int size = ((1 << (index + 6)) / 2);
    node *toSplit =(char *)temporary + size; //where error occurs

    if(temporary->next != NULL) {
        fList[index+1] = temporary->next;
        temporary->next= NULL;
        fList[index+1]->previous = NULL;
    }
    else{
        fList[index+1]=NULL;
    }   

    temporary->next = toSplit;
    toSplit->previous = temporary;

    if(fList[index] != NULL){
        toSplit->next = fList[index];
        fList[index]->previous=toSplit;
    }
    else{
        toSplit->next = NULL;
    }

    fList[index] = temporary;
    temporary->previous=NULL;

    temporary->header = index+5;
    toSplit->header = index+5;


    if(fList[index]->header == baseCase+5){
        fList[index]->header |=128;
        void *tmp = fList[index];
        fList[index]=fList[index]->next;
        if(fList[index]!=NULL)
        {
            fList[index]->previous=NULL;    
        }
        return tmp;
    }
    else{
        divider(index-1,baseCase);
    }
}

Upvotes: 0

Views: 807

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49920

Error pretty much says it: you have cast temporary to be a different type of pointer than the variable node that you are trying to assign it to.

As for the incompatability part, there are typically rules for where structs can go in memory (the address has to be a multiple of usually 4 or 8), but chars aren't so restricted, and so you can have a char in a location where a node cannot be.

As for fixing it: cast it to the proper type, and make sure you are generating a valid address for your node.

Upvotes: 1

Related Questions