我有一个名为仓库的结构和一个通用链表,每个项目都指向一个仓库结构。
typedef struct linked{
char type;
void * item;
struct linked * next;
struct linked * prev;
}LinkedList;
typedef struct warehouse{
char * name;
float volume;
float (* getPrice) (void * S);
float (* getTotalDollarAmount)(void * S);
}house_t;
当我试图让getPrice
函数指针指向一个函数时float price (void *S)
void menu (LinkedList *house){
char *c;
float num;
c = (char*)malloc(sizeof(char)*10);
LinkedList *i;
i = (LinkedList*)malloc(sizeof(LinkedList);
house_t *sk;
sk = (house_t *) malloc(sizeof(house_t));
//i->item = (house_t *) malloc(sizeof(house_t));
scanf("%c", c);
((house_t*)i->item)->getPrice = price;
sk=findhouse(house, c);
num = ((house_t*)i->item)->getPrice(sk);
printf("%f",num);
}
我遇到了错误的访问错误。因为每次我遇到错误的访问错误都是因为我没有为某些东西分配内存。但是我需要为函数指针分配内存吗?如果是这样,怎么做?
这里还有一些代码
float price (void *S)
{
return ((house_t*)S)->volume;
}