Re: pointers...

Discussion in 'C Programming' started by Emmanuel Delahaye, Jun 25, 2003.

  1. In 'comp.lang.c', "geo" <geometrikal@hhoottmmaaiill> wrote:

    > struct random_struct {
    > .....
    > .....
    > .....
    > }
    >
    > struct random_struct random;
    >
    > and then i had a function which used this structure. What is the best
    > programming practice:
    >
    > void function(struct random_struct * r) {
    > .......
    > }
    >
    > function(&random);


    Yes, or

    void function(struct random_struct const * r) {

    for read-only access.

    > or:
    >
    > void function(struct random_struct r) {
    > .......
    > }
    >
    > function(random);


    No.

    > and why?


    Because the structure is copied. Can be a way to protect the original, but
    'const' with a pointer is enough for that. The copy can be dangerous (the
    automatic memory is small) and it's slow ad useless.

    However it is acceptable for small ojects (one or two int), specially for
    return.

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-library: http://www.dinkumware.com/htm_cl/index.html
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    --
    comp.lang.c.moderated - moderation address:
    Emmanuel Delahaye, Jun 25, 2003
    #1
    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:
    646
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    761
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    456
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    687
  5. cerr

    pointers, pointers, pointers...

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

Share This Page