void argument

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PengYu.UT@gmail.com

    void argument

    Hi,

    I know that there is a difference (in C) between

    int fun(void);

    int fun();

    The first one takes no arguments. The second can take any number of
    arguments, including 0.

    I'm wondering if this difference is still hold in C++.

    Thanks,
    Peng

  • Jacques Labuschagne

    #2
    Re: void argument

    PengYu.UT@gmail .com wrote:[color=blue]
    > Hi,
    >
    > I know that there is a difference (in C) between
    >
    > int fun(void);
    >
    > int fun();
    >
    > The first one takes no arguments. The second can take any number of
    > arguments, including 0.
    >
    > I'm wondering if this difference is still hold in C++.[/color]

    Nope, they're exactly the same thing in C++: fun takes no arguments. For
    a function taking any number of arguments use ellipses, e.g. void fun(...);

    Jacques.

    Comment

    • Cy Edmunds

      #3
      Re: void argument

      <PengYu.UT@gmai l.com> wrote in message
      news:1128811195 .034220.292210@ g44g2000cwa.goo glegroups.com.. .[color=blue]
      > Hi,
      >
      > I know that there is a difference (in C) between
      >
      > int fun(void);
      >
      > int fun();
      >
      > The first one takes no arguments. The second can take any number of
      > arguments, including 0.
      >
      > I'm wondering if this difference is still hold in C++.
      >
      > Thanks,
      > Peng
      >[/color]

      No, in C++ they mean the same: a function which takes no arguments. The
      business about taking any number of arguments is disallowed in C++. Because
      of overloading, the argument types are part of the function's signature.

      --
      Cy



      Comment

      • Nan Li

        #4
        Re: void argument

        No.

        [nan@athena test]$ cat test2.c
        void foo()
        {
        }
        int main()
        {
        foo(1,2);
        return 1;
        }
        [nan@athena test]$ g++ -Wall -x c test2.c
        [nan@athena test]$ g++ -Wall -x c++ test2.c
        test2.c: In function `int main()':
        test2.c:2: error: too many arguments to function `void foo()'
        test2.c:7: error: at this point in file

        Comment

        • Chris Hills

          #5
          Re: void argument

          In article <1128812307.838 583.9210@f14g20 00cwb.googlegro ups.com>, Nan Li
          <nan.li.g@gmail .com> writes[color=blue]
          >No.[/color]

          No what?

          If you are replying top something quote the message you are replying to.

          If you are using the broken google interface to usenet please set the
          options properly.




          --
          \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
          \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
          /\/\/ chris@phaedsys. org www.phaedsys.org \/\/\
          \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



          Comment

          • ben

            #6
            Re: void argument

            PengYu.UT@gmail .com wrote:[color=blue]
            > Hi,
            >
            > I know that there is a difference (in C) between
            >
            > int fun(void);
            >
            > int fun();
            >
            > The first one takes no arguments. The second can take any number of
            > arguments, including 0.
            >
            > I'm wondering if this difference is still hold in C++.
            >
            > Thanks,
            > Peng
            >[/color]


            No. In C++, the above two prototypes are exactly the same.

            Ben

            Comment

            • Clark S. Cox III

              #7
              Re: void argument

              On 2005-10-08 18:48:19 -0400, Jacques Labuschagne
              <jacques@clawsh rimp.com> said:
              [color=blue]
              > PengYu.UT@gmail .com wrote:[color=green]
              >> Hi,
              >>
              >> I know that there is a difference (in C) between
              >>
              >> int fun(void);
              >>
              >> int fun();
              >>
              >> The first one takes no arguments. The second can take any number of
              >> arguments, including 0.
              >>
              >> I'm wondering if this difference is still hold in C++.[/color]
              >
              > Nope, they're exactly the same thing in C++: fun takes no arguments.
              > For a function taking any number of arguments use ellipses, e.g. void
              > fun(...);[/color]

              Actually, no. You cannot define a function taking any number of
              arguments that way. Using the ellipsis (even in C), requires at least
              one non variadic parameter, i.e.:

              /* This function requires at least one int argument, but can take any
              number of additional, non-POD arguments */
              void fun(int, ...)
              {
              }

              /* This function definition is illegal:*/
              void fun2(...)
              {
              }

              --
              Clark S. Cox, III
              clarkcox3@gmail .com

              Comment

              • Alf P. Steinbach

                #8
                Re: void argument

                * Clark S. Cox III:[color=blue]
                > On 2005-10-08 18:48:19 -0400, Jacques Labuschagne
                > <jacques@clawsh rimp.com> said:
                >[color=green]
                > > PengYu.UT@gmail .com wrote:[color=darkred]
                > >> Hi,
                > >>
                > >> I know that there is a difference (in C) between
                > >>
                > >> int fun(void);
                > >>
                > >> int fun();
                > >>
                > >> The first one takes no arguments. The second can take any number of
                > >> arguments, including 0.
                > >>
                > >> I'm wondering if this difference is still hold in C++.[/color]
                > >
                > > Nope, they're exactly the same thing in C++: fun takes no arguments.
                > > For a function taking any number of arguments use ellipses, e.g. void
                > > fun(...);[/color]
                >
                > Actually, no. You cannot define a function taking any number of
                > arguments that way. Using the ellipsis (even in C), requires at least
                > one non variadic parameter, i.e.:[/color]

                Not in C++ (I don't know about C).

                There are two forms of parameter-declaration-clause,

                parameter-declaration-list[OPT] ...[OPT]
                parameter-declaration-list, ...

                It seems you've focused only the second form.

                You might ask, what would something like void foo(...){} be good for, since
                there's no argument there to anchor the argument retrieval?

                And one answer is that the ellipsis ... ranks lowest of all possible matches
                for an actual argument, and for this usage it would just be silly to require
                an extra argument in front.

                --
                A: Because it messes up the order in which people normally read text.
                Q: Why is it such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?

                Comment

                • Branimir Maksimovic

                  #9
                  Re: void argument


                  <PengYu.UT@gmai l.com> wrote in message
                  news:1128811195 .034220.292210@ g44g2000cwa.goo glegroups.com.. .[color=blue]
                  > Hi,
                  >
                  > I know that there is a difference (in C) between
                  >
                  > int fun(void);
                  >
                  > int fun();
                  >
                  > The first one takes no arguments. The second can take any number of
                  > arguments, including 0.
                  >
                  > I'm wondering if this difference is still hold in C++.[/color]

                  No, and that is a very good thing.
                  When I was C beginner, that lack of prototyping in C was a real problem
                  for me (and I dind't know that one should always prototype functions
                  back in 1987.).

                  int main()
                  {
                  int i = malloc(32);
                  int *p = malloc(32);
                  free(i);
                  free(p);
                  return 0;
                  }

                  This C code compiles and links without problem, but causes lot of trouble.

                  Greetings, Bane.


                  Comment

                  • Rolf Magnus

                    #10
                    Re: void argument

                    Alf P. Steinbach wrote:
                    [color=blue]
                    > You might ask, what would something like void foo(...){} be good for,
                    > since there's no argument there to anchor the argument retrieval?[/color]

                    My question would be how to retrieve the arguments, since va_start needs the
                    last fixed argument, which in this case doesn't exist.

                    Comment

                    • Bob Hairgrove

                      #11
                      Re: void argument

                      On Sun, 09 Oct 2005 00:53:30 GMT, alfps@start.no (Alf P. Steinbach)
                      wrote:
                      [color=blue]
                      >You might ask, what would something like void foo(...){} be good for, since
                      >there's no argument there to anchor the argument retrieval?[/color]

                      I've only seen this syntax used in catch statements (i.e. as a
                      "catch-all" last resort). It is sometimes dangerous, though --
                      depending on the platform, it could be a source of memory leaks if
                      used indiscriminatel y.

                      --
                      Bob Hairgrove
                      NoSpamPlease@Ho me.com

                      Comment

                      • Alf P. Steinbach

                        #12
                        Re: void argument

                        * Rolf Magnus:[color=blue]
                        > Alf P. Steinbach wrote:
                        >[color=green]
                        > > You might ask, what would something like void foo(...){} be good for,
                        > > since there's no argument there to anchor the argument retrieval?[/color]
                        >
                        > My question would be how to retrieve the arguments, since va_start needs the
                        > last fixed argument, which in this case doesn't exist.[/color]

                        That's the same question; already answered. ;-)

                        --
                        A: Because it messes up the order in which people normally read text.
                        Q: Why is it such a bad thing?
                        A: Top-posting.
                        Q: What is the most annoying thing on usenet and in e-mail?

                        Comment

                        • Jay Nabonne

                          #13
                          Re: void argument

                          On Sat, 08 Oct 2005 15:58:27 -0700, Nan Li wrote:
                          [color=blue]
                          > No.
                          >
                          > [nan@athena test]$ cat test2.c
                          > void foo()
                          > {
                          > }
                          > int main()
                          > {
                          > foo(1,2);
                          > return 1;
                          > }
                          > [nan@athena test]$ g++ -Wall -x c test2.c
                          > [nan@athena test]$ g++ -Wall -x c++ test2.c
                          > test2.c: In function `int main()':
                          > test2.c:2: error: too many arguments to function `void foo()'
                          > test2.c:7: error: at this point in file[/color]

                          There's a difference between a function prototype ("declaratio n") and a
                          function definition.

                          Change the above "foo" to:

                          extern void foo();

                          and it will work fine. The assumption is that there is a real function
                          somewhere that has some sort of arguments. The danger is you're not
                          matching the real argument definition, so the compiler has to guess based
                          on the arguments you pass.

                          Of course, that's all C, so I may have a detail or two wrong...

                          - Jay

                          Comment

                          • Branimir Maksimovic

                            #14
                            Re: void argument


                            "Jay Nabonne" <jay_nabonne@so nicNOSPAM.com> wrote in message
                            news:pan.2005.1 0.09.19.55.51.1 6000@sonicNOSPA M.com...[color=blue]
                            > On Sat, 08 Oct 2005 15:58:27 -0700, Nan Li wrote:
                            >[color=green]
                            >> No.
                            >>
                            >> [nan@athena test]$ cat test2.c
                            >> void foo()
                            >> {
                            >> }
                            >> int main()
                            >> {
                            >> foo(1,2);
                            >> return 1;
                            >> }
                            >> [nan@athena test]$ g++ -Wall -x c test2.c
                            >> [nan@athena test]$ g++ -Wall -x c++ test2.c
                            >> test2.c: In function `int main()':
                            >> test2.c:2: error: too many arguments to function `void foo()'
                            >> test2.c:7: error: at this point in file[/color]
                            >
                            > There's a difference between a function prototype ("declaratio n") and a
                            > function definition.
                            >
                            > Change the above "foo" to:
                            >
                            > extern void foo();
                            >
                            > and it will work fine. The assumption is that there is a real function
                            > somewhere that has some sort of arguments. The danger is you're not
                            > matching the real argument definition, so the compiler has to guess based
                            > on the arguments you pass.
                            >
                            > Of course, that's all C, so I may have a detail or two wrong...[/color]

                            function definition with empty parenthesis in c also means no arguments.
                            this is valid only for declaration, so you are right, but g++ would compile
                            it
                            as c++ anyway; one must use gcc for c compiling.

                            void foo();

                            int main()
                            {
                            foo(1);
                            foo(1,'c',3.2);
                            return 0;
                            }

                            void foo()
                            {
                            }

                            compiles with no problem in c.

                            Greetings, Bane.



                            Comment

                            Working...