home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!wirzeniu
- From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
- Newsgroups: comp.lang.c
- Subject: Re: Help with function typedef.
- Message-ID: <1992Aug15.094138.24793@klaava.Helsinki.FI>
- Date: 15 Aug 92 09:41:38 GMT
- References: <1992Aug14.223459.2638@ultra.com>
- Organization: University of Helsinki
- Lines: 42
-
- jimh@ultra.com (Jim Hurley) writes:
- >typedef unsigned char CLI_RETURN_CODE;
- >typedef CLI_RETURN_CODE (*CLI) (int, char **);
-
- This defines `CLI' as a `pointer to function returning unsigned char and
- having two arguments, an int and a pointer-to-pointer-to-char'. Note
- the difference between function and pointer-to-function.
-
- >/* prototype */
- >CLI cli_help;
-
- I get the feeling that this tries to create a prototype for a function
- `cli_help', but it does in fact define only a pointer to a function.
-
- >CLI_RETURN_CODE cli_help (int argc, char **argv) {
-
- This defines `cli_help', which was already defined above as a pointer to
- a function as a function. Since pointers and functions are different,
- this correctly results in an error. Probably the correct way to fix
- things is to write the prototype to be
-
- CLI_RETURN_CODE cli_help (int argc, char **argv);
-
- After this, the name `cli_help' can be used to initialize pointers of
- type CLI, due to the fact that the name of a function, when used alone
- and not for calling the function, is a pointer to the function (not
- quite unlike the fact that the name of an array is a pointer to its
- first element, in value contexts).
-
- >x.c:17: initializer for static variable is not constant
-
- I lost track of lines, but I think this message was for the
- initializion of the structure (which I deleted), one field of which was
- a pointer to a function initialized with `cli_help'. Since `cli_help'
- was a pointer with a non-constant value, it is an error to use its value
- to initialize a global or static variable. By changing the definition
- of `cli_help' as a pointer into a prototype for a function we also make
- the initializion legal, since now the value of `cli_help' (a pointer to
- a function) is constant.
-
- --
- Lars.Wirzenius@helsinki.fi
-