English 中文(简体)
我能否把结构变成一个无效的职能?
原标题:Can I pass a structure into a function that takes void*?

既然如此, 我应该能够通过某种类型的权利? 为什么编辑者给我留下错误?

int cmp_func(void *, void *));

typedef struct word_{
  char key[WORD_SIZE];
  int *frequency;
} word;

Phash_table new_hash(int size, int (*hash_func)(char *), int (*cmp_func)(void *
, void *));

int comp_function(struct word_ *word1,struct word_ *word2){
  if( word1->frequency < word2->frequency){
    return -1;
  }
  if(word1->frequency <  word2->frequency){
      return 1;
  }
  if(word1->frequency == word2->frequency){
    return 0;
  }
}

project4_functions.c:47:3: warning: passing argument 3 of  new_hash  from incompatible pointer type [enabled by default]
hash.h:38:13: note: expected  int (*)(void *, void *)  but argument is of type  int (*)(struct word_ *, struct word_ *) 
最佳回答

关键在于使你的比较功能也带有空白点:

int comp_function(void *a, void *b){
  struct word *word1 = a;
  struct word *word2 = b;
  // Use word1 and word2 as before.
}

增编:why 编辑们向各位发出警告:

To quote the c99 standard which I found here

A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

这意味着,你可以有以下守则,汇编者则发出警告:

void *a = NULL;
int (*f)(int a, char *b) = NULL;
a = f;
f = a;

它试图推断,这意味着(毕竟,我们只是将“避免*”改为“停止*”?

int (*f1)(void*, void*);
int (*f2)(struct foo*, struct foo*);
f1 = f2;

然而,这引起了你的警示,因为它是而不是,试图按照标准的规定,将某一点的类型指定为无效点(或副面)。 相反,它试图将以下类型的数值分配到:int (*)(struct foo*, ruct foo*)的变式int (*)(撤销*、无效*)

当然,您的could试图使汇编者对明确表述感到高兴,这让汇编者相信,你必须知道你的工作。 但是,在这样做时,即使援引“扩散”行为,你也失去了获得这些警告的特权和安全。

问题回答

你的问题与你的法典不符。 你的法典没有通过一个结构点作为真空点。 它是另一个职能点。 职能点不兼容,因此错误。

It is legal to pass a structure pointer where a void pointer is expected because a structure pointer can be implicitly converted to a void pointer. It is not required to be representationally identical to a void pointer. (There are some machines where structure pointers are not the same size as a void pointer, for example.)

通过类比,在预计时间长时考虑过暗中的情况。 这样做是合法的,因为有隐含的转换,但这意味着接受的固定功能与长期接受的职能是互换的。

由于你的职能原型与预期的职能不匹配,你需要把职能点推到:

typedef int (cmp_f)(void *, void *));
new_hash(..., ..., (cmp_f*)cmp_func_p);

Of course that typedef is not necessary, but it makes your code much more readable than without (you usually only do it without that typedef in exams where you are not allowed to use typedef for this purpose ;))





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...