Re: Can function prototypes appear anywhere?

Discussion in 'C Programming' started by Lawrence Kirby, Jan 20, 2005.

  1. On Wed, 19 Jan 2005 14:09:55 +0100, Grumble wrote:

    > Hello everyone,
    >
    > I've come across some strange code. Here it is, stripped down:
    >
    > int main(void)
    > {
    > int *foo;
    > int *bar();
    > foo = bar(0);
    > return 0;
    > }
    >
    > $ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
    > /* NO WARNING */
    >
    > If I understand correctly, line 2 in main() is a function prototype


    It is a declaration but it isn't a prototype. Prototypes are a later
    addition to the C language derived from C++. In a prototype form of
    declaration you specify type information within the ()'s of the parameter
    list. So in your code above int main(void) is a prototype (definitions can
    be prototypes) but int *bar(); isn't.

    Lawrence





    > which declares 'bar' as a function taking an unspecified number of
    > parameters, and returning a pointer to int?
    >
    > Can function prototypes really appear anywhere in the code?
    >
    > I suppose the better way to write that code would be:
    >
    > int *bar();
    > int main(void)
    > {
    > int *foo;
    > foo = bar(0);
    > return 0;
    > }
    >
    > Would you agree?
    Lawrence Kirby, Jan 20, 2005
    #1
    1. Advertising

  2. Lawrence Kirby

    Grumble Guest

    Lawrence Kirby wrote:

    > On Wed, 19 Jan 2005 14:09:55 +0100, Grumble wrote:
    >
    >> I've come across some strange code. Here it is, stripped down:
    >>
    >> int main(void)
    >> {
    >> int *foo;
    >> int *bar();
    >> foo = bar(0);
    >> return 0;
    >> }
    >>
    >> $ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
    >> /* NO WARNING */
    >>
    >> If I understand correctly, line 2 in main() is a function prototype

    >
    > It is a declaration but it isn't a prototype. Prototypes are a later
    > addition to the C language derived from C++. In a prototype form of
    > declaration you specify type information within the ()'s of the parameter
    > list. So in your code above int main(void) is a prototype (definitions can
    > be prototypes) but int *bar(); isn't.


    Point taken.

    Would you write

    int main(void)
    {
    int *foo;
    int *bar(int);
    foo = bar(0);
    return 0;
    }

    or would you declare bar() outside main() ?

    --
    Regards, Grumble
    Grumble, Jan 20, 2005
    #2
    1. Advertising

  3. On Thu, 20 Jan 2005 15:39:44 +0100, Grumble wrote:

    ....

    > Would you write
    >
    > int main(void)
    > {
    > int *foo;
    > int *bar(int);
    > foo = bar(0);
    > return 0;
    > }
    >
    > or would you declare bar() outside main() ?


    Outside main(), either static if bar() is defined in the same source file
    or in a header otherwise.

    Lawrence
    Lawrence Kirby, Jan 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    1
    Views:
    382
    Scott Allen
    May 6, 2005
  2. Denis Remezov

    order of const in function prototypes

    Denis Remezov, Jun 30, 2004, in forum: C++
    Replies:
    7
    Views:
    432
    John Harrison
    Jun 30, 2004
  3. Bo Jacobsen
    Replies:
    6
    Views:
    510
    David M. Cook
    Mar 7, 2004
  4. Chris Dollin

    Re: Can function prototypes appear anywhere?

    Chris Dollin, Jan 19, 2005, in forum: C Programming
    Replies:
    3
    Views:
    353
    Michael Wojcik
    Jan 19, 2005
  5. Syren Baran

    function prototypes and function addresses

    Syren Baran, Jan 9, 2008, in forum: C Programming
    Replies:
    6
    Views:
    307
Loading...

Share This Page