Re: pointers...

Discussion in 'C Programming' started by E. Robert Tisdale, Jun 25, 2003.

  1. geo wrote:

    > say I had:
    >
    > typedef struct random_struct {
    > // . . .
    > // . . .
    > // . . .
    > } random_struct;
    >
    > random_struct random;
    >
    > and then I had a function which used this structure.
    > What is the best programming practice:
    >
    > void function(const random_struct* r) { // better
    > // . . .
    > }
    >
    > function(&random);
    >
    > or:
    >
    > void function(random_struct r) { // not so good
    > // . . .
    > }
    >
    > function(random);
    >
    > and why?


    Try to eliminate unnecessary copies for large objects.
    pass a reference (through a pointer)
    instead of passing the the object by value.
    --
    comp.lang.c.moderated - moderation address:
     
    E. Robert Tisdale, Jun 25, 2003
    #1
    1. Advertising

  2. "E. Robert Tisdale" wrote:
    >

    <snip>

    > Try to eliminate unnecessary copies for large objects.
    > pass a reference (through a pointer)
    > instead of passing the the object by value.



    In C, /all/ passing of parameters is done by value. If you aren't
    passing it by value, you aren't passing it at all.

    Please do try not to confuse the OP with incorrect statements. Thanks.


    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Jun 25, 2003
    #2
    1. Advertising

  3. E. Robert Tisdale

    Kevin Easton Guest

    Richard Heathfield <> wrote:
    > "E. Robert Tisdale" wrote:
    >>

    > <snip>
    >
    >> Try to eliminate unnecessary copies for large objects.
    >> pass a reference (through a pointer)
    >> instead of passing the the object by value.

    >
    >
    > In C, /all/ passing of parameters is done by value. If you aren't
    > passing it by value, you aren't passing it at all.


    A pointer value is a reference to the object pointed-to; I believe
    something along these lines is even in the standard. So "pass a
    reference (through a [parameter of] pointer [type])" seems fine.

    - Kevin.
     
    Kevin Easton, Jun 25, 2003
    #3
  4. E. Robert Tisdale

    dbtid Guest

    On Wed, 25 Jun 2003 13:17:28 GMT, Kevin Easton <>
    wrote:

    > Richard Heathfield <> wrote:
    >> "E. Robert Tisdale" wrote:
    >>>

    >> <snip>
    >>
    >>> Try to eliminate unnecessary copies for large objects.
    >>> pass a reference (through a pointer)
    >>> instead of passing the the object by value.

    >>
    >>
    >> In C, /all/ passing of parameters is done by value. If you aren't
    >> passing it by value, you aren't passing it at all.

    >
    > A pointer value is a reference to the object pointed-to; I believe
    > something along these lines is even in the standard. So "pass a
    > reference (through a [parameter of] pointer [type])" seems fine.
    >
    > -Kevin.


    Well, not to put too fine a point on it...

    Other languages, most notably Pascal, have the concepts of pass by
    reference, pass by value, AND pointers.

    I think what Richard was saying is that even a pointer, while it can
    be used to mimic pass-by-reference behaviour as seen in other languages, is
    really different. C++ has references, and they're called
    that. C strictly has pass-by-value, regardless of the type of the
    parameter.

    Again, not trying to split hairs or anything.
     
    dbtid, Jun 25, 2003
    #4
  5. E. Robert Tisdale

    dbtid Guest

    On Wed, 25 Jun 2003 13:42:25 GMT, Kevin Easton <>
    wrote:

    > dbtid <> wrote:
    >> On Wed, 25 Jun 2003 13:17:28 GMT, Kevin Easton <kevin@-nospam-
    >> pcug.org.au> wrote:
    >>
    >>> Richard Heathfield <> wrote:
    >>>> "E. Robert Tisdale" wrote:
    >>>>>
    >>>> <snip>
    >>>>
    >>>>> Try to eliminate unnecessary copies for large objects.
    >>>>> pass a reference (through a pointer)
    >>>>> instead of passing the the object by value.
    >>>>
    >>>>
    >>>> In C, /all/ passing of parameters is done by value. If you aren't
    >>>> passing it by value, you aren't passing it at all.
    >>>
    >>> A pointer value is a reference to the object pointed-to; I believe
    >>> something along these lines is even in the standard. So "pass a
    >>> reference (through a [parameter of] pointer [type])" seems fine.
    >>>
    >>> -Kevin.

    >>
    >> Well, not to put too fine a point on it...
    >>
    >> Other languages, most notably Pascal, have the concepts of pass by
    >> reference, pass by value, AND pointers.
    >>
    >> I think what Richard was saying is that even a pointer, while it can
    >> be used to mimic pass-by-reference behaviour as seen in other languages,
    >> is really different. C++ has references, and they're called
    >> that. C strictly has pass-by-value, regardless of the type of the
    >> parameter.

    >
    > I realise that, and it's perfectly true that C does not have
    > pass-by-reference function call semantics. However, you can still pass
    > *a* reference (different to pass *by* reference), bearing in mind that
    > the meaning of "reference" in C++ is apparently not the same as it is in
    > C.
    >
    > -Kevin.


    Here, I'm going to agree with you... Mr Tisdale could have probably said
    something very similar to what you and I have both stated, and gotten no
    other response.

    As it was, his original statements were misleading to a new C programmer,
    and Richard called him on it. Then you disagreed with him, I disagreed
    with you, and now we're both in agreement.

    Oh, joy, what a wonderful day!
     
    dbtid, Jun 25, 2003
    #5
  6. E. Robert Tisdale

    Kevin Easton Guest

    Joe Wright <> wrote:
    [...]
    > Sorry, it's too early to agree. Passing parameters by 'reference' is a
    > language thing and is supported by C++ and other languages. Not in C!


    Yes, C does not have pass-by-reference function call semantics.

    > One might think that a pointer value that 'points' to an object is a
    > 'reference' to the object. Maybe in English but not in C or other
    > programming languages.


    Yes, in C. I refer you to 6.2.5 in C99, paragraph 19:

    6.2.5
    19 . A pointer type may be derived from a function type, an object type,
    or an incomplete type, called the referenced type. A pointer type
    describes an object whose value provides a reference to an entity of the
    referenced type.

    - Kevin.
     
    Kevin Easton, Jun 26, 2003
    #6
  7. E. Robert Tisdale

    Simon Biber Guest

    "Kevin Easton" <> wrote:
    > Yep. I see no reason to buy a copy of the final version
    > merely for answering questions on comp.lang.c.


    That's your choice, of course, but in future please indicate
    the source correctly when quoting from it.

    I didn't buy my copy solely for answering questions on c.l.c
    as I also use it when programming in C myself.

    --
    Simon.
     
    Simon Biber, Jun 26, 2003
    #7
  8. E. Robert Tisdale

    Kevin Easton Guest

    Simon Biber <> wrote:
    > "Kevin Easton" <> wrote:
    >> Yep. I see no reason to buy a copy of the final version
    >> merely for answering questions on comp.lang.c.

    >
    > That's your choice, of course, but in future please indicate
    > the source correctly when quoting from it.


    Sometimes I will say N869, sometimes I won't feel the need to.

    > I didn't buy my copy solely for answering questions on c.l.c
    > as I also use it when programming in C myself.


    I also see no reason to buy a C99 compiler.

    - Kevin.
     
    Kevin Easton, Jun 26, 2003
    #8
  9. E. Robert Tisdale

    Simon Biber Guest

    "Kevin Easton" <> wrote:
    > Sometimes I will say N869, sometimes I won't feel the need to.


    Since you don't know whether the wording may have been changed
    significantly between versions (and it has in some sections),
    it's always prudent to indicate that you're quoting from a draft
    version.

    > I also see no reason to buy a C99 compiler.


    If the C99 compiler (Comeau) and C99 library (Dinkumware)
    cost as little as I paid for the C99 standard (US$18) then
    I would buy them right now.

    --
    Simon.
     
    Simon Biber, Jun 26, 2003
    #9
  10. E. Robert Tisdale

    Dan Pop Guest

    In <newscache$3tg1hh$b1b$> Kevin Easton <> writes:

    >Richard Heathfield <> wrote:
    >> "E. Robert Tisdale" wrote:
    >>>

    >> <snip>
    >>
    >>> Try to eliminate unnecessary copies for large objects.
    >>> pass a reference (through a pointer)
    >>> instead of passing the the object by value.

    >>
    >>
    >> In C, /all/ passing of parameters is done by value. If you aren't
    >> passing it by value, you aren't passing it at all.

    >
    >A pointer value is a reference to the object pointed-to; I believe
    >something along these lines is even in the standard. So "pass a
    >reference (through a [parameter of] pointer [type])" seems fine.


    It may seem fine, but it may also be confusing, especially to people
    familiar with languages actually supporting passing by reference (think
    about C++ references).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 26, 2003
    #10
  11. On Thu, 26 Jun 2003 03:50:30 GMT, in comp.lang.c , Kevin Easton
    <> wrote:

    >I also see no reason to buy a C99 compiler.


    Luddite.
    In a couple of years, you'll have no choice. Better learn while its
    still optional !

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Jun 26, 2003
    #11
  12. E. Robert Tisdale

    Joe Wright Guest

    Kevin Easton <> wrote in message
    news:newscache$eqf2hh$iw5$...
    > Joe Wright <> wrote:
    > [...]
    > > Sorry, it's too early to agree. Passing parameters by 'reference' is

    a
    > > language thing and is supported by C++ and other languages. Not in

    C!
    >
    > Yes, C does not have pass-by-reference function call semantics.
    >
    > > One might think that a pointer value that 'points' to an object is a
    > > 'reference' to the object. Maybe in English but not in C or other
    > > programming languages.

    >
    > Yes, in C. I refer you to 6.2.5 in C99, paragraph 19:
    >
    > 6.2.5
    > 19 . A pointer type may be derived from a function type, an object

    type,
    > or an incomplete type, called the referenced type. A pointer type
    > describes an object whose value provides a reference to an entity of

    the
    > referenced type.
    >

    And I contend that, in context here, reference is an English word and
    has nothing whatever to do with 'by value' or 'by reference' parameter
    passing. And you know that.
     
    Joe Wright, Jun 27, 2003
    #12
  13. E. Robert Tisdale

    Richard Bos Guest

    Mark McIntyre <> wrote:

    > On Thu, 26 Jun 2003 02:59:18 GMT, in comp.lang.c , Kevin Easton
    > <> wrote:
    >
    > >Yep. I see no reason to buy a copy of the final version merely for
    > >answering questions on comp.lang.c.

    >
    > other than to avoid confusion by either
    > a) making an incorrect statement (there were a couple of small changes
    > post final draft); and
    > b) referencing the correct sections...
    >
    > Come on, its only eighteen bucks.


    ....plus the cost of a credit card. And I don't just mean the initial
    monetary cost, either.

    Richard
     
    Richard Bos, Jun 27, 2003
    #13
  14. E. Robert Tisdale

    Kevin Easton Guest

    Joe Wright <> wrote:
    >
    > Kevin Easton <> wrote in message
    > news:newscache$eqf2hh$iw5$...
    >> Joe Wright <> wrote:
    >> [...]
    >> > Sorry, it's too early to agree. Passing parameters by 'reference' is

    > a
    >> > language thing and is supported by C++ and other languages. Not in

    > C!
    >>
    >> Yes, C does not have pass-by-reference function call semantics.
    >>
    >> > One might think that a pointer value that 'points' to an object is a
    >> > 'reference' to the object. Maybe in English but not in C or other
    >> > programming languages.

    >>
    >> Yes, in C. I refer you to 6.2.5 in C99, paragraph 19:
    >>
    >> 6.2.5
    >> 19 . A pointer type may be derived from a function type, an object

    > type,
    >> or an incomplete type, called the referenced type. A pointer type
    >> describes an object whose value provides a reference to an entity of

    > the
    >> referenced type.
    >>

    > And I contend that, in context here, reference is an English word and
    > has nothing whatever to do with 'by value' or 'by reference' parameter
    > passing. And you know that.


    Yes, I do (know it has nothing to do with pass *by* reference), which is
    why I was very careful to stress the wording pass *a* reference.

    - Kevin.
     
    Kevin Easton, Jun 27, 2003
    #14
  15. E. Robert Tisdale

    Micah Cowan Guest

    (Richard Bos) writes:

    > Mark McIntyre <> wrote:
    >
    > > On Thu, 26 Jun 2003 02:59:18 GMT, in comp.lang.c , Kevin Easton
    > > <> wrote:
    > >
    > > >Yep. I see no reason to buy a copy of the final version merely for
    > > >answering questions on comp.lang.c.

    > >
    > > other than to avoid confusion by either
    > > a) making an incorrect statement (there were a couple of small changes
    > > post final draft); and
    > > b) referencing the correct sections...
    > >
    > > Come on, its only eighteen bucks.

    >
    > ...plus the cost of a credit card. And I don't just mean the initial
    > monetary cost, either.


    What, no debit card?

    -Micah
     
    Micah Cowan, Jun 27, 2003
    #15
  16. E. Robert Tisdale

    Richard Bos Guest

    Micah Cowan <> wrote:

    > (Richard Bos) writes:
    >
    > > Mark McIntyre <> wrote:
    > >
    > > > On Thu, 26 Jun 2003 02:59:18 GMT, in comp.lang.c , Kevin Easton
    > > > <> wrote:
    > > >
    > > > >Yep. I see no reason to buy a copy of the final version merely for
    > > > >answering questions on comp.lang.c.
    > > >
    > > > other than to avoid confusion by either
    > > > a) making an incorrect statement (there were a couple of small changes
    > > > post final draft); and
    > > > b) referencing the correct sections...
    > > >
    > > > Come on, its only eighteen bucks.

    > >
    > > ...plus the cost of a credit card. And I don't just mean the initial
    > > monetary cost, either.

    >
    > What, no debit card?


    Not over the 'net, no.

    Richard
     
    Richard Bos, Jun 30, 2003
    #16
    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. Phil
    Replies:
    1
    Views:
    675
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    785
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    481
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    711
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    739
Loading...

Share This Page