每次运行此代码时,我都会取消引用不完整的类型

时间:2011-10-25 18:58:46

标签: c pointers

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

typedef void withdrawPtr(int);
typedef void depositPtr(int);
typedef void accountPtr(int);
typedef void deltaccountPtr();
int balance;
pthread_mutex_t mutex1;

我实际上已将等效的c ++代码转换为c代码

typedef struct 
{
   int (*read)();
   withdrawPtr *withdraw;
   depositPtr *deposit;
   accountPtr *account;
   deltaccountPtr *deltaccount;

} accountstrct;


 void  *WithdrawThread(void *param)
{
struct accountstrct*  Mystruct = (struct accountstrct*) param;

这里我得去解除指向不完整类型的指针。我没有得到这个函数在这里退缩的其他方式。

Mystruct->withdrawPtr=*withdraw ;
Mystruct->withdrawPtr(2);
return 0;
   }

2 个答案:

答案 0 :(得分:3)

您从未定义过struct accountstrct。将类型定义为struct accountstrct { };并使用struct accountstrct引用类型或将类型定义为typedef struct {} accountstrct;并使用accountstrct引用类型(不是“struct accountscrct”)。

您目前定义了一个名为accountstrct的类型,但您尝试使用名为struct accountstrct的类型。

答案 1 :(得分:0)

我不确定你是如何编译这些typedef的。将*放在那里,并使它们成为正确的函数指针定义。然后从*字段中取出struct。在底部,Mystruct没有withdrawPtr字段,您可能只是withdraw。说到哪,withdraw来自哪里?

这里的内容应该更像:

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

typedef void (*withdrawPtr)(int);
typedef void (*depositPtr)(int);
typedef void (*accountPtr)(int);
typedef void (*deltaccountPtr)();
int balance;
pthread_mutex_t mutex1;

typedef struct 
{
   int (*read)();
   withdrawPtr withdraw;
   depositPtr deposit;
   accountPtr account;
   deltaccountPtr deltaccount;

} accountstrct;


void  *WithdrawThread(void *param)
{
  struct accountstrct*  Mystruct = (struct accountstrct*) param;
...

  Mystruct->withdraw = withdraw;
  Mystruct->withdraw(2);
  return 0;
}