开发者

How does this function definition work?

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:

  1. I thought C++ was a superset of C. If its so, this should compile with a C++ compiler as well right?
  2. How does the C compiler understand the definition? str and size are undeclared when they first appear.
  3. What is the purpose of declaring str and size after function signature but before function body rather than following the开发者_开发技巧 normal approach of doing it in either of the two places?
  4. 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++.


The Old C syntax for the declaration of a function's formal arguments is still supported by some compilers.

For example

int func (x) 
int x 
{

}

is old style (K&R style) syntax for defining a function.

I thought C++ was a superset of C. If its so, this should compile with a C++ compiler as well right?

Nopes! C++ is not a superset of C. This style(syntax) of function declaration/definition was once a part of C but has never been a part of C++. So it shouldn't compile with a C++ compiler.


This appears to be "old-school" C code. Declaring the types of the parameters outside of the parentheses but before the open curl-brace of the code block is a relic of the early days of C programming (I'm not sure why but I guess it has something to do with variable management on the stack and/or compiler design).

To answer your questions:

  1. Calling C++ a "superset" of C is somewhat a misnomer. While they share basic syntax features, and you can even make all sorts of C library calls from C++, they have striking differences with respect to type safety, warnings vs. errors (C is more permissible), and compiler/preprocessor options.

  2. Most contemporary C compilers understand legacy code (such as this appears to be). The C compiler holds the function parameter names sort of like "placeholders" until their type can be declared immediately following the function header name.

  3. No real "purpose" other than again, this appears to be ancient code, and the style back in the day was like this. The "normal" approach is IMO the better, more intuitive way.

  4. My suggestion:

    unsigned int hash(register char *str, register unsigned int size) { // Definition }

A word of advice: Consider abandoning the register keyword - this was used in old C programs as a way of specifying that the variable would be stored in a memory register (for speed/efficiency), but nowadays compilers are better at optimizing away this need. I believe that modern compilers ignore it. Also, you cannot use the & (address of) operator in C/C++ on a register variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜