Tik*_*hon 1 c prototype compiler-errors function
我是一名新手程序员,我希望得到一些帮助,解释为什么以下实现非常简单的哈希函数会返回编译错误:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define LENGTH 45
int hash(char word[LENGTH+1]);
int main(void)
{
char word[LENGTH+1];
strcpy(word, "HelloWorld");
//print out hash
int hash = hash(word);
// = ( ( (int) word[1] * (int) word[2]) % 1000 );
printf("%i\n", hash);
return 0;
}
int hash(char word[LENGTH+1])
{
int hash = ( ( (int) word[1] * (int) word[2]) % 1000 );
return hash;
}
Run Code Online (Sandbox Code Playgroud)
编译器返回消息:
test3.c:25:24:错误:调用对象类型'int'不是函数或函数指针
简单地在main中执行我的哈希函数作为一行代码而不是原型函数是很容易的,但如果有人能够解释为什么这不起作用,我将非常感激.
您声明了一个与函数同名的变量:
int hash = hash(word);
Run Code Online (Sandbox Code Playgroud)
变量和函数在C中位于相同的命名空间中,因此声明变量会影响函数.因此,当编译器看到时hash(word),它会抱怨您正在尝试将其int用作函数.
其中一个使用不同的名称.
int hashcode = hash(word);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |