C-无法转换';void*';至';b树&x27;物件
我有一个创建免费BTree的代码,但第6行有一个错误,它说C-无法转换';void*';至';b树&x27;物件,c,malloc,b-tree,C,Malloc,B Tree,我有一个创建免费BTree的代码,但第6行有一个错误,它说无法将void转换为object如何修复它 bTree btCreate(void) { bTree b; b = malloc(sizeof(*b)); // '=':cannot convert 'void *' to 'bTree' assert(b); b->isLeaf = 1; b->numKeys = 0; return b; } 有什么想法吗? 谢谢。假设bTree被声明为: typedef st
无法将void转换为object
如何修复它
bTree btCreate(void)
{
bTree b;
b = malloc(sizeof(*b)); // '=':cannot convert 'void *' to 'bTree'
assert(b);
b->isLeaf = 1;
b->numKeys = 0;
return b;
}
有什么想法吗?
谢谢。假设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;
}
#包括
#包括
类型定义结构
{
国际岛;
int numKeys;
}b树;
bTree*btCreate(void)
{
b树*b;
b=malloc(sizeof(bTree));//注意这里的sizeof(bTree)
如果(b==NULL)
{
printf(“malloc失败\n”);
返回NULL;
}
//初始化
b->isLeaf=1;
b->numKeys=0;
返回b;
}
int main()
{
b树*p树;
ptree=btCreate();
if(ptree!=NULL){
printf(“初始值:\n”);
printf(“isLeaf=%d\n”,ptree->isLeaf);
printf(“numKeys=%d\n”,ptree->numKeys);
}
返回0;
}
如果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;
}
#包括
#包括
类型定义结构
{
国际岛;
int numKeys;
}b树;
bTree*btCreate(void)
{
b树*b;
b=malloc(sizeof(bTree));//注意这里的sizeof(bTree)
如果(b==NULL)
{
printf(“malloc失败\n”);
返回NULL;
}
//初始化
b->isLeaf=1;
b->numKeys=0;
返回b;
}
int main()
{
b树*p树;
ptree=btCreate();
if(ptree!=NULL){
printf(“初始值:\n”);
printf(“isLeaf=%d\n”,ptree->isLeaf);
printf(“numKeys=%d\n”,ptree->numKeys);
}
返回0;
}
希望这有帮助什么是
bTree
?那么bTree
是指针的typedef?请不要这样做。这是现有typedef最糟糕的用法之一。指针语义需要明确。你自己已经在努力了。用C编译器代替C++编译器…或b=(b树)malloc(sizeof(*b))代码>。所有其他注释仍然适用。什么是bTree
?那么bTree
是指针的typedef?请不要这样做。这是现有typedef最糟糕的用法之一。指针语义需要明确。你自己已经在努力了。用C编译器代替C++编译器…或b=(b树)malloc(sizeof(*b))代码>。所有其他意见仍然适用。