[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: porting PAM



>>> "sven_dehmlow@gmx.de" 28-May-03 13:29 >>>
>
> On Tue, 27 May 2003 invad3rkim@flexcheck.net wrote:
>
> > > On Tue, 27 May 2003 invad3rkim@flexcheck.net wrote:
> > >
> [...]
> >
> > Thanks. That is an excellent article. This is a little unrelated to
> > my initial post, but I have a question about C in general. I've seen
> > function definitions like this a lot: int syscall(int, char *)
> >
> > I don't really understand how function prototypes can get away with
> > only giving the data type and not a name for it's arguments. How are
> > these referenced from within the function?

(Not sure why this is on tech@ ... And donning my flame-proof suit for
all the slight inaccuracies that people will no doubt roast me for...)

Err... that's fairly standard C.

Make sure you know the difference between a function declaration, a
function definition, and a function invocation.

Function invocation is calling a function:

  rv = strlcpy(dest, src, sizeof(dest)) ;


Functions should be prototyped before being called.  This tells the
compiler what types of arguments should be expected in each position,
so that the compiler can tell you if you make a mistake when calling
the function.  Functions can be declared either with or without names
for the parameters, but they must have types.  Functions are typically
declared in header files (.h), except for static functions, which are
usually declared at the top of the module (.c file) they are used in:

   size_t strlcpy(char *dst, const char *src, size_t size) ;

   -or-

   size_t strlcpy(char *, const char *, size_t) ;


Functions need to be defined so they exist. (!)  Parameters must be
given names so that they can be referred to within the function.
There are two ways of doing this, the original method and the ANSI
method.  The original K&R way of doing this listed the names of the
paramters and then re-listed the names with the types:

   size_t
   strlcpy(dst, src, size)
      char *dst ;
      const char *src ;    /* const not avail in trad. C */
      size_t size ;
   {
      /* Implementation goes here */
   }


The ANSI method of specifying the function header is much better:

   size_t strlcpy(char *dst, const char *src, size_t size)
   {
      /* Implementation goes here */
   }


Note that function declarations have a ; after the closing ) following
the parameters (i.e. no function body).

My personal preference is for the first style of declaration, including
names for the parameters, since this can be created by a quick YP in vi
from the first line of the function definition; just add a ";" at the
end.


And with that all behind us, to directly answer your question:

> > I don't really understand how function prototypes can get away with
> > only giving the data type and not a name for it's arguments. How are
> > these referenced from within the function?

That's the difference between a function prototype/declaration, and the
definition of the function.  The function body refers to paramters based
on the first line of its definition.  The compiler should complain if
the first line of the definition does not match the declaration (if any).

Hope this helps

Tom