函数的 C typedef,如何使用 - 简单示例

标签 c function typedef

我会展示我的意思:

标题:

#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/

相关文章:

python - 用python计算csv文件中的列

javascript - Function.prototype.call 在严格模式之外改变 this 的类型;为什么?

c - 调用泛型函数时如何有效处理参数

c++ - 如何使用 typedef 完全隐藏特定类型?

c++ - 无法访问 Arduino 结构字段。错误 "exit status 1. xxxx does not name a type"

c - _ ("write error") 是什么意思?

c - 用 C 编写函数,传递参数

c - C 中的 typedef、数组和指针

C语言编程 printf

c++ - libespeak 在消息末尾发出额外的音节