我会展示我的意思:
标题:
#ifndef _HASH_H_
#define _HASH_H_
typedef void* pKey;
typedef int (*HashFunc) (pKey key, int size);
#endif
新标题:
#ifndef _DICT_H_
#define _DICT_H_
#include "hash.h"
HashFunc HashWord;
#endif
现在我不知道该写什么,我想自己写HashWord
函数
c文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "dict.h"
typedef struct _wordElement {
char* word;
char* translation
} wordElement, *pwordElement;
HashFunc HashWord{
......... here i will want to write the Code
}
现在它给我一个错误,我应该如何写最后一行?
也许
HashFunc HashWord(pKey theKey,int number){...}
也许
HashFunc HashWord( theKey, number){...}
也许
int HashWord (pKey key, int size){...}
什么是正确的方法?
最佳答案
i want to write the HashWord function itself
但是 HashWord
不是一个函数,它是一个函数指针。定义一个匹配函数指针签名的函数,HashFunc
,并将函数地址赋值给HashWord
。例如:
int HashFunc_1 (pKey key, int size)
{
return 0;
}
int HashFunc_2 (pKey key, int size)
{
return 0;
}
pKey k = ...;
HashWord = HashFunc_1;
HashWord(k, 4);
HashWord = HashFunc_2;
HashWord(k, 4);
关于函数的 C typedef,如何使用 - 简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27565076/