Fixing old C-style code

This is a discussion on Fixing old C-style code within the C Programming forums, part of the General Programming Boards category; I'm trying to update some old code that won't compile as is. A big problem seems to be function declarations. ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    3

    Fixing old C-style code

    I'm trying to update some old code that won't compile as is. A big problem seems to be function declarations. I see them declared with regular variables at the beginning of the function, such as:

    Code:
    startrun() {
       double GetParameters();
       double num; 
    
       num = GetParameters("in"); 
       // etc
    }
    Producing a compilation error of:
    Code:
    code-sim3.C:343: too many arguments to function `double GetParameters()'
    code-sim3.C:361: at this point in file
    This style occurs whether the function being called is in the same file as the calling function or if it is in a different file.

    Another thing I'm seeing that I'm not familiar with is the following use of braces and semicolons:
    Code:
     {long time(); (Global->computestart) = time(0);};
    Which, in addition to the excess braces and semicolons, also produces the same problem as above (declaring the use of a function followed by the actual use of it) and results in the same compilation error.

    Finally, what is the point of a string of braces and semicolons, such as:
    Code:
    {;};
    {;};
    {;};
    Thanks! I really appreciate any help I can get on this subject!

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    The complier errors are pretty much self explainatory. There is no problem with the style of declaration. Your prototype for function GetParameters(), tell complier that it takes no argument, however when you call the function you pass a string "in" to it, which is causing complier to give out that error and bail out.

    Fix it either by changing the code in prototype or in the function call.

    Also, i think simple ";" means nothing but a blank stmt which does nothing. Its harmless.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > double GetParameters();
    Are you sure you're compiling as C code, and not C++? Because from what I've read this would mean GetParameters takes an unknown number of arguments, which while not the preferred method, is valid.

    You can always substitute in the preferred prototype, but it shouldn't be necessary.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    You're correct, I am compiling as C++ code, but I'm correcting old c-code for use in new suite written in C++.

    As for the prototype stuff, I always thought the term was for the statements at the beginning of the file or in a header file which specify the functions present in the file. I am unfamiliar with the usage I described, in which a function is simply stated (with no parameters, as if to say: "I'm going to use this function later on) at the beginning of each function in which it is called.

    Thanks for your help!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well there's your problem then. Empty parameters, as swoopy stated, do not mean the same thing in C as they do in C++. This:
    Code:
    void foo();
    Means it has any number, or no, parameters in one, but in the other it means it has none, ever. Therefore, you get the same problem here:
    Code:
    { /* Start a new code block. */
        long time(); /* prototype the function 'time' wich returns a 'long',
                        and takes no arguments */
        (Global->computestart) = time(0); /* call time with an argument. oops. */
    };  /* End this code block. The ; does nothing */

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    Aha, I get it. I removed the initial prototypes and it works. Thanks a lot guys!

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > I removed the initial prototypes and it works.
    Whereas fixing the prototypes would have been better than merely giving the compiler less information so it shuts up about some things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21