Thread: Weird function declarations

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    Weird function declarations

    I've come across some code that was a real hack job, and was written by a real hacker. I'm not exactly sure what his function declarations are doing:
    Code:
    docmd_w(cm,cmd,dr)
    char cm;
    char *cmd;
    int dr;
    {
      char c,t;
      char *p,*s;
      int st,r,i,j,l,ch;
    ...
    I had assumed that this would be equivalent to
    Code:
    int docmd_w(char cm, char *cmd, int dr)
    {
     ...
    but GCC disagrees (in the form of error messages if I change the function declaration). Can someone explain the subtleties of declaring functions his way?
    Away.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your ANSI-ized version looks correct. What kind of errors are you getting? Are you sure there's not a return type on the line above the name of the function?

    By the way, that "odd" style of function declaration is called K&R, after the original developers of C. ANSI is the group that introduced the new way of function declarations involving prototypes, etc.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    You don't tell us what error message the GCC is giving you. If there is a forward function declaration in K&R style, ex:
    Code:
    int docmd_w();
    in a header file, or earlier in the program file, GCC is complaining about the different definitions.

    Check the header files, and earlier in the code file itself, for such forward declarations.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    There are no prototypes anywhere that I can find for most of the functions (all the ones without a return type listed), but there is the comment
    Code:
    /***DECLARATIONS***/
    /* declare non-int functions
     */
    The error messages I get are along the lines of these:
    Code:
    adio.c: At top level:
    adio.c:509: error: conflicting types for docmd_w
    adio.c:509: note: an argument type that has a default promotion can't match an empty parameter name list declaration
    adio.c:390: error: previous implicit declaration of docmd_w was here
    make: *** [adio.o] Error 1
    Away.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'd look at line 390 and make sure it matches your declaration on line 509 then...
    If you understand what you're doing, you're not learning anything.

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    The function isn't defined at line 390; it's simply used. The return value and variables passed are consistent with my declaration - returns an int, and is passed a char, char* and an int. I'll post the files later tonight if I still can't figure it out (I'm on a different computer at the moment, and don't have time to copy the files over, so I was just using SSH).
    Away.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's why then. You're using the function before it's declared. In that case your compiler makes assumptions about the function. Try adding a function prototype somewhere in your file before it's used.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM