2017-01-18 5 views
0

無料のBTreeを作成するコードがありますが、どうすれば修正できるのですか?C - 'void *'をオブジェクト 'bTree'に変換することはできません

bTree btCreate(void) 
{ 
bTree b; 


b = malloc(sizeof(*b)); // '=':cannot convert 'void *' to 'bTree' 
assert(b); 

b->isLeaf = 1; 
b->numKeys = 0; 

return b; 

} 

ありがとう。

+2

と* 'bTree' *何ですか? –

+2

'bTree'はポインタのtypedefですか?しないでください。これは存在するtypedefの最悪の使用の1つです。ポインタのセマンティクスは明示的にする必要があります。あなた自身はすでにそれを抱えています。 – StoryTeller

+2

C++コンパイラの代わりにCコンパイラを使用します。 – BLUEPIXY

答えて

3

BTREEは次のように宣言されていると仮定:

typedef struct 
{ 
int isLeaf; 
int numKeys; 
}bTree; 

これはあなたの関数の呼び出しの例です。

#include <stdio.h> 
#include <stdlib.h> 

typedef struct 
{ 
    int isLeaf; 
    int numKeys; 
}bTree; 

bTree* btCreate(void) 
{ 
    bTree *b; 

    b = malloc(sizeof(bTree)); //pay attention here sizeof(bTree) 
    if (b==NULL) 
    { 
    printf ("malloc failed \n"); 
    return NULL; 
    } 
    //initialization 
    b->isLeaf = 1; 
    b->numKeys = 0; 

return b; 
} 

int main() 
{ 
    bTree* ptree; 
    ptree = btCreate(); 

    if(ptree!=NULL){ 
    printf ("initial values:\n"); 
    printf ("isLeaf = %d \n",ptree->isLeaf); 
    printf ("numKeys = %d \n",ptree->numKeys); 
    } 
    return 0; 
} 

・ホープ、このヘルプ