home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12370 < prev    next >
Encoding:
Internet Message Format  |  1992-08-14  |  2.1 KB

  1. Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!wirzeniu
  2. From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help with function typedef.
  5. Message-ID: <1992Aug15.094138.24793@klaava.Helsinki.FI>
  6. Date: 15 Aug 92 09:41:38 GMT
  7. References: <1992Aug14.223459.2638@ultra.com>
  8. Organization: University of Helsinki
  9. Lines: 42
  10.  
  11. jimh@ultra.com (Jim Hurley) writes:
  12. >typedef unsigned char CLI_RETURN_CODE;
  13. >typedef CLI_RETURN_CODE (*CLI) (int, char **);
  14.  
  15. This defines `CLI' as a `pointer to function returning unsigned char and
  16. having two arguments, an int and a pointer-to-pointer-to-char'.  Note
  17. the difference between function and pointer-to-function.
  18.  
  19. >/* prototype */
  20. >CLI cli_help;
  21.  
  22. I get the feeling that this tries to create a prototype for a function
  23. `cli_help', but it does in fact define only a pointer to a function.
  24.  
  25. >CLI_RETURN_CODE cli_help (int argc, char **argv) {
  26.  
  27. This defines `cli_help', which was already defined above as a pointer to
  28. a function as a function.  Since pointers and functions are different,
  29. this correctly results in an error.  Probably the correct way to fix
  30. things is to write the prototype to be
  31.  
  32.     CLI_RETURN_CODE cli_help (int argc, char **argv);
  33.  
  34. After this, the name `cli_help' can be used to initialize pointers of
  35. type CLI, due to the fact that the name of a function, when used alone
  36. and not for calling the function, is a pointer to the function (not
  37. quite unlike the fact that the name of an array is a pointer to its
  38. first element, in value contexts). 
  39.  
  40. >x.c:17: initializer for static variable is not constant
  41.  
  42. I lost track of lines, but I think this message was for the
  43. initializion of the structure (which I deleted), one field of which was
  44. a pointer to a function initialized with `cli_help'.  Since `cli_help'
  45. was a pointer with a non-constant value, it is an error to use its value
  46. to initialize a global or static variable.  By changing the definition
  47. of `cli_help' as a pointer into a prototype for a function we also make
  48. the initializion legal, since now the value of `cli_help' (a pointer to
  49. a function) is constant.
  50.  
  51. --
  52. Lars.Wirzenius@helsinki.fi
  53.