请问这个函数定义工作? [英] How does this function definition work?
问题描述
我生成具有的gperf
前两天的哈希函数。我所看到的散
功能是陌生的我。它是这样的(我不记得确切的语法):
无符号整型
哈希(STR,大小)
注册的char * str中;
注册unsigned int类型的大小;
{
//定义
}
现在,当我试图用C ++编译器(G ++)就向我扔的错误不具有 STR
,并编制尺寸
声明。但是,这对编译C编译器(GCC)。所以,问题:
- 我认为C ++是C的超集,如果它是这样,这应该用C ++编译器编译,以及对吗?
- 如何C编译器理解定义?
STR
和尺寸
是不宣而当他们第一次出现。 - 什么是按照正常的声明的目的
STR
和尺寸
后的函数签名而在此之前的函数体,而不是无论是在两地做这件事的方法呢? - 如何得到这个功能来编译g ++的,所以我可以在我的C ++ code使用它呢?或者我应该尝试的gperf从生成C ++ code?这可能吗?
1  C ++是不是一个超集,虽然这不是标准的C或者。
2/3。这是K&安培; R函数声明。见<一href=\"http://stackoverflow.com/questions/22500/what-are-the-major-differences-between-ansi-c-and-kr-c\">What是ANSI C和K&放大器之间的主要区别; R C?
。
&4 NBSP;的gperf事实上确实有一个选项, -L
,以指定的语言。你可以只用 -L C ++
来使用C ++。
I generated a hash function with gperf
couple of days ago. What I saw for the hash
function was alien to me. It was something like this (I don't remember the exact syntax) :
unsigned int
hash(str, size)
register char* str;
register unsigned int size;
{
//Definition
}
Now, when I tried to compile with a C++ compiler (g++) it threw errors at me for not having str
and size
declared. But this compiled on the C compiler (gcc). So, questions:
- I thought C++ was a superset of C. If its so, this should compile with a C++ compiler as well right?
- How does the C compiler understand the definition?
str
andsize
are undeclared when they first appear. - What is the purpose of declaring
str
andsize
after function signature but before function body rather than following the normal approach of doing it in either of the two places? - How do I get this function to compile on g++ so I can use it in my C++ code? Or should I try generating C++ code from gperf? Is that possible?
1. C++ is not a superset, although this is not standard C either.
2/3. This is a K&R function declaration. See What are the major differences between ANSI C and K&R C? .
4. gperf does in fact have an option, -L
, to specify the language. You can just use -L C++
to use C++.
这篇关于请问这个函数定义工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!