void function(int &a) { }

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    void function(int &a) { }

    I saw this code in C++ but when tried to C causes an error:
    -------------
    void function(int &a) { a = 5; }
    -------------
    with this, passed in "function" the "a" pointer instead of "a" value, BUT
    inside "function" I have access to "a" value not with "*a" but with "a".

    Why this is not working in C?


  • Greg P.

    #2
    Re: void function(int &a) { }

    "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message
    news:bgasrt$c7l $1@nic.grnet.gr ...[color=blue]
    > I saw this code in C++ but when tried to C causes an error:
    > -------------
    > void function(int &a) { a = 5; }
    > -------------[/color]

    You are trying to pass by reference, which is C++ specific. You can try two
    things, if you don't want to make a copy of 'a' (which an int is hardly
    anything to worry about):

    void function(int *a) { *a = 5; }

    or just remove the ampersand altogether

    I won't get into references here as this newsgroup is for the C standard,
    not C++


    Comment

    • Chris Dollin

      #3
      Re: void function(int &amp;a) { }

      - Chameleon - wrote:
      [color=blue]
      > I saw this code in C++ but when tried to C causes an error:
      > -------------
      > void function(int &a) { a = 5; }
      > -------------
      > with this, passed in "function" the "a" pointer instead of "a" value, BUT
      > inside "function" I have access to "a" value not with "*a" but with "a".
      >
      > Why this is not working in C?[/color]

      Because C++ isn't the same language as C, so there's no obvious
      reason why a C++ feature would work in C.

      If you want to use C, learn C.

      --
      Chris "electric hedgehog" Dollin
      C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
      C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html

      Comment

      • Dan Pop

        #4
        Re: void function(int &amp;a) { }

        In <bgasrt$c7l$1@n ic.grnet.gr> "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> writes:
        [color=blue]
        >I saw this code in C++ but when tried to C causes an error:
        >-------------
        >void function(int &a) { a = 5; }
        >-------------
        >with this, passed in "function" the "a" pointer instead of "a" value, BUT
        >inside "function" I have access to "a" value not with "*a" but with "a".
        >
        >Why this is not working in C?[/color]

        Why isn't Fortran syntax working in C?
        Why isn't Pascal syntax working in C?
        Why isn't Perl syntax working in C?
        And so on, and so on, and so on...

        Next time, try engaging your brain before posting a question. It doesn't
        (or, at least, shouldn't) hurt.

        Dan
        --
        Dan Pop
        DESY Zeuthen, RZ group
        Email: Dan.Pop@ifh.de

        Comment

        • Martin Ambuhl

          #5
          Re: void function(int &amp;a) { }

          Greg P. wrote:

          [color=blue]
          > I won't get into references here as this newsgroup is for the C standard,
          > not C++[/color]

          No, this newsgroup is for standard C. The newsgroup for the C standard
          is news:comp.std.c



          --
          Martin Ambuhl

          Comment

          • Greg P.

            #6
            Re: void function(int &amp;a) { }

            "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
            news:2b8Wa.71$k P6.52@newsread2 .news.atl.earth link.net...
            | No, this newsgroup is for standard C. The newsgroup for the C standard
            | is news:comp.std.c
            That's what i meant, give a guy some slack =)


            Comment

            • Greg P.

              #7
              Re: void function(int &amp;a) { }

              "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
              news:q38Wa.59$k P6.47@newsread2 .news.atl.earth link.net...
              | Because it's nonsense. '(int &a)' is a syntax error. If you want to do
              | this in C, write in C instead of using the pointless syntactic sugar
              | introduced in C++ for people too stupid to write C:
              I half agree with that statement as I find templates a break from thousands
              of lines of code (which are only C++). But I also find that references are a
              lazy way of lacking pointer knowledge.


              Comment

              • Guest's Avatar

                #8
                Re: void function(int &amp;a) { }

                > The sytnax is not C language syntax. You yourself pointed out that it's
                from[color=blue]
                > a different language. Here are some other things that won't work:
                >
                > $a =~ s/\d+/$1+1/eg;
                > 0 -72 -72 0 0 72 72 0 4 72 72 moveto { rlineto } repeat stroke
                > if (a instanceof java.lang.Strin g) System.out.prin tln((String) a);
                >
                > ... and so on ...[/color]

                ha ha! Ok! I thought that maybe it is standard C but I was wrong!
                Thanks!
                [color=blue][color=green]
                > > I saw this code in C++ but when tried to C causes an error:
                > > -------------
                > > void function(int &a) { a = 5; }
                > > -------------
                > > with this, passed in "function" the "a" pointer instead of "a" value,[/color][/color]
                BUT[color=blue][color=green]
                > > inside "function" I have access to "a" value not with "*a" but with "a".
                > >
                > > Why this is not working in C?[/color][/color]


                Comment

                • Guest's Avatar

                  #9
                  Re: void function(int &amp;a) { }

                  > >I saw this code in C++ but when tried to C causes an error:[color=blue][color=green]
                  > >-------------
                  > >void function(int &a) { a = 5; }
                  > >-------------
                  > >with this, passed in "function" the "a" pointer instead of "a" value, BUT
                  > >inside "function" I have access to "a" value not with "*a" but with "a".
                  > >
                  > >Why this is not working in C?[/color]
                  >
                  > Why isn't Fortran syntax working in C?
                  > Why isn't Pascal syntax working in C?
                  > Why isn't Perl syntax working in C?
                  > And so on, and so on, and so on...[/color]

                  Its clearly now ;-)
                  [color=blue]
                  > Next time, try engaging your brain before posting a question. It doesn't
                  > (or, at least, shouldn't) hurt.[/color]

                  be cool


                  Comment

                  • Slartibartfast

                    #10
                    Re: void function(int &amp;a) { }

                    "Greg P." <no@spam.sam> wrote in message news:1w8Wa.712$ jg7.180@newsrea d3.news.pas.ear thlink.net...[color=blue]
                    > "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
                    > news:q38Wa.59$k P6.47@newsread2 .news.atl.earth link.net...
                    > | Because it's nonsense. '(int &a)' is a syntax error. If you want to do
                    > | this in C, write in C instead of using the pointless syntactic sugar
                    > | introduced in C++ for people too stupid to write C:
                    > I half agree with that statement as I find templates a break from thousands
                    > of lines of code (which are only C++). But I also find that references are a
                    > lazy way of lacking pointer knowledge.
                    >
                    >[/color]

                    Yes, of course C++ reference syntax is nonsense in a C program, but to describe it as "pointless syntactic sugar" or "a lazy way of
                    lacking pointer knowledge [sic]" demonstrates your ignorance of C++.

                    --
                    #include <stdio.h>
                    char*f="#includ e <stdio.h>char*f =%c%s%c;main(){ printf(f,34,f,3 4,10);}%c";
                    main(){printf(f ,34,f,34,10);}


                    Comment

                    • goose

                      #11
                      Re: void function(int &amp;a) { }

                      "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message news:<bgasrt$c7 l$1@nic.grnet.g r>...[color=blue]
                      > I saw this code in C++ but when tried to C causes an error:
                      > -------------
                      > void function(int &a) { a = 5; }
                      > -------------
                      > with this, passed in "function" the "a" pointer instead of "a" value, BUT
                      > inside "function" I have access to "a" value not with "*a" but with "a".
                      >
                      > Why this is not working in C?[/color]

                      because C is not C++.

                      C does not support pass-by-reference function calls. presumably,
                      C++ does.

                      the above in C would be
                      void function (int *a) { *a = 5; }

                      hth
                      goose,

                      Comment

                      • Martin Ambuhl

                        #12
                        Re: void function(int &amp;a) { }

                        Greg P. wrote:
                        [color=blue]
                        > "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
                        > news:q38Wa.59$k P6.47@newsread2 .news.atl.earth link.net...
                        > | Because it's nonsense. '(int &a)' is a syntax error. If you want to do
                        > | this in C, write in C instead of using the pointless syntactic sugar
                        > | introduced in C++ for people too stupid to write C:[/color]
                        [color=blue]
                        > I half agree with that statement as I find templates a break from thousands
                        > of lines of code (which are only C++). But I also find that references are a
                        > lazy way of lacking pointer knowledge.[/color]

                        While templates can be useful, there were no templates in the posted
                        code to which I responded. The only C++ syntactic sugar in evidence was
                        the so-called "pass-by-reference."




                        --
                        Martin Ambuhl

                        Comment

                        • Cousin Ricky

                          #13
                          Re: void function(int &amp;a) { }

                          "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message news:<bgasrt$c7 l$1@nic.grnet.g r>...[color=blue]
                          > I saw this code in C++ but when tried to C causes an error:
                          > -------------
                          > void function(int &a) { a = 5; }
                          > -------------
                          > with this, passed in "function" the "a" pointer instead of "a" value, BUT
                          > inside "function" I have access to "a" value not with "*a" but with "a".
                          >
                          > Why this is not working in C?[/color]

                          Perhaps because C is not C++. Just a guess.

                          --
                          ------------------- Richard Callwood III --------------------
                          ~ U.S. Virgin Islands ~ USDA zone 11 ~ 18.3N, 64.9W ~
                          ~ eastern Massachusetts ~ USDA zone 6 (1992-95) ~
                          --------------- http://cac.uvi.edu/staff/rc3/ ---------------

                          Comment

                          • bd

                            #14
                            Re: void function(int &amp;a) { }

                            On Thu, 31 Jul 2003 14:02:32 +0300, <- Chameleon -> wrote:
                            [color=blue]
                            > I saw this code in C++ but when tried to C causes an error:
                            > -------------
                            > void function(int &a) { a = 5; }
                            > -------------
                            > with this, passed in "function" the "a" pointer instead of "a" value, BUT
                            > inside "function" I have access to "a" value not with "*a" but with "a".
                            >
                            > Why this is not working in C?[/color]

                            Because C isn't C++.

                            --
                            Freenet distribution not available
                            Chicken Little was right.

                            Comment

                            • Default User

                              #15
                              Re: void function(int &amp;a) { }



                              Martijn wrote:[color=blue]
                              >
                              > <- Chameleon -> wrote:[color=green]
                              > > Why this is not working in C?[/color]
                              >
                              > Why all these offensive responses? It is a legitimate question which only
                              > Greg seems to take this seriously. All those "witty" responses are more
                              > cluttering than the original post.[/color]

                              How do you figure that it is a legitimate question? Even the most
                              cursory of research would have revealed that C doesn't support
                              references.
                              [color=blue]
                              > I guess it is true what they say: "There are no stupid questions, only
                              > stupid answers" :)[/color]

                              Besides stupid questions and stupid answers there are stupid statements.




                              Brian Rodenborn

                              Comment

                              Working...