Pointer to struct or Struct parameters

Discussion in 'C Programming' started by Fernan Bolando, Aug 7, 2004.

  1. What is the best way of passing a structure to a function currently I do
    by it passing a pointer to the structe.

    sample code below

    int main()
    {
    struct sample_struct a_struct, *point_struct;

    point_struct = &a_struct;

    a_function(a_struct);
    /* maybe do some stuff */
    return 0;
    }

    int a_function(struct sample_struct *point_local)
    {
    /* do some stuff here using the pointer */

    return 0;
    }


    Is there any better way of doing this?

    thanks
    ,Fernan

    --
    Public PGP/GnuPG key (http://www.fernski.com)
    pub 1024D/681A5498 2003-10-26 Fernan Bolando
    Key fingerprint = BAC8 EA6A CA98 26F3 3DB4 74D1 988E 56F7 681A 5498
    Fernan Bolando, Aug 7, 2004
    #1
    1. Advertising

  2. Fernan Bolando

    -berlin.de Guest

    Fernan Bolando <> wrote:
    > What is the best way of passing a structure to a function currently I do
    > by it passing a pointer to the structe.


    > sample code below


    > int main()
    > {
    > struct sample_struct a_struct, *point_struct;


    > point_struct = &a_struct;


    > a_function(a_struct);


    Here you're passing a copy of the structure to the function, not a
    pointer to it as it's required from the the way you define the
    called function (if you have set your compiler to a reasonable
    warning level and have a declaration of the function is scope it
    will flag this). If this wasn't a typo you should become aware of
    the fact that, in contrast to arrays, you can pass whole structures
    to functions - when used without a dot and an element name following
    it a structure does _not_ gets converted (decays) to a pointer to
    the structure. So here you would need either

    a_function( point_struct );
    or
    a_function( &a_struct );

    Since you don't seem to need the 'point_struct' variable for
    anything else I would tend to get rid of it and use the second
    way because you don't have to double-check that 'point_struct'
    is really pointing to the structure you want to pass to the
    function.

    > /* maybe do some stuff */
    > return 0;
    > }


    > int a_function(struct sample_struct *point_local)
    > {
    > /* do some stuff here using the pointer */


    > return 0;
    > }


    > Is there any better way of doing this?


    Beside what probably was just a typo, there's no better way, it just
    depends on what you want to do. If you don't want to change the
    structure within the function you can also pass the structure itself
    (i.e. a copy of it) instead of a pointer to the function. This has
    the advantage that you can't make the mistake of changing anything
    in the structure involuntarily, but can have the disadvantage that
    it may be quite a bit slower because the whole structure must be
    copied instead of just a pointer each time the function is called.

    On the other hand, if you want to change the contents of the structure
    from within the function (and don't want to return the structure itself
    from the function), then there's no other way than passing a pointer to
    the structure to the function.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 7, 2004
    #2
    1. Advertising

  3. Fernan Bolando

    CBFalconer Guest

    Fernan Bolando wrote:
    >
    > What is the best way of passing a structure to a function currently I do
    > by it passing a pointer to the structe.
    >
    > sample code below
    >
    > int main()
    > {
    > struct sample_struct a_struct, *point_struct;
    >
    > point_struct = &a_struct;
    >
    > a_function(a_struct);

    ^^^^^^^^^^^
    This function is undefined at this point. Syntax error.

    > /* maybe do some stuff */
    > return 0;
    > }
    >
    > int a_function(struct sample_struct *point_local)
    > {
    > /* do some stuff here using the pointer */
    >
    > return 0;
    > }
    >
    > Is there any better way of doing this?


    Yes. Take the whole definition of a_function and move it ahead of
    main, so it is defined when called. At this point you will get an
    error on the type of the parameter, which you can correct either
    by defining the parameter to be a struct sample_struct, or by call
    with a pointer.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Aug 7, 2004
    #3
  4. In article <>,
    -berlin.de wrote:
    > Fernan Bolando <> wrote:
    >> What is the best way of passing a structure to a function currently I do
    >> by it passing a pointer to the structe.

    >
    >> sample code below

    >
    >> int main()
    >> {
    >> struct sample_struct a_struct, *point_struct;

    >
    >> point_struct = &a_struct;

    >
    >> a_function(a_struct);

    >
    > Here you're passing a copy of the structure to the function, not a
    > pointer to it as it's required from the the way you define the
    > called function (if you have set your compiler to a reasonable
    > warning level and have a declaration of the function is scope it
    > will flag this). If this wasn't a typo you should become aware of


    Yes, This was a typo I really meant to pass the pointer to the function.

    > it a structure does _not_ gets converted (decays) to a pointer to
    > the structure. So here you would need either
    >
    > a_function( point_struct );
    > or
    > a_function( &a_struct );
    >
    > Since you don't seem to need the 'point_struct' variable for
    > anything else I would tend to get rid of it and use the second
    > way because you don't have to double-check that 'point_struct'
    > is really pointing to the structure you want to pass to the
    > function.


    Is there any situation where it would be advantagoues to use the first
    option?.

    >> /* maybe do some stuff */
    >> return 0;
    >> }

    >
    >> int a_function(struct sample_struct *point_local)
    >> {
    >> /* do some stuff here using the pointer */

    >
    >> return 0;
    >> }

    >
    >> Is there any better way of doing this?

    >
    > Beside what probably was just a typo, there's no better way, it just
    > depends on what you want to do. If you don't want to change the
    > structure within the function you can also pass the structure itself
    > (i.e. a copy of it) instead of a pointer to the function. This has
    > the advantage that you can't make the mistake of changing anything
    > in the structure involuntarily, but can have the disadvantage that
    > it may be quite a bit slower because the whole structure must be
    > copied instead of just a pointer each time the function is called.
    >
    > On the other hand, if you want to change the contents of the structure
    > from within the function (and don't want to return the structure itself
    > from the function), then there's no other way than passing a pointer to
    > the structure to the function.


    My current situation is the function needs to change the contents of the
    structure. The structure actually contains a linked list of integers and
    float that is dynamically allocated by the called function.

    I am just trying to avoid making the function global.


    thanks for the quick response


    ,Fernan


    --
    Public PGP/GnuPG key (http://www.fernski.com)
    pub 1024D/681A5498 2003-10-26 Fernan Bolando
    Key fingerprint = BAC8 EA6A CA98 26F3 3DB4 74D1 988E 56F7 681A 5498
    Fernan Bolando, Aug 8, 2004
    #4
  5. Fernan Bolando

    pete Guest

    Fernan Bolando wrote:

    > My current situation is the function needs
    > to change the contents of the structure.


    The regular way for a function to change the value of an object,
    is to use a pointer to the object as an argument to the function.

    If you passed the value of the object to the function,
    then you would have to have the new value returned by the function
    and assigned to the object in question.
    pete, Aug 8, 2004
    #5
  6. Fernan Bolando

    -berlin.de Guest

    Fernan Bolando <> wrote:
    > In article <>,
    > -berlin.de wrote:
    >> it a structure does _not_ gets converted (decays) to a pointer to
    >> the structure. So here you would need either
    >>
    >> a_function( point_struct );
    >> or
    >> a_function( &a_struct );
    >>
    >> Since you don't seem to need the 'point_struct' variable for
    >> anything else I would tend to get rid of it and use the second
    >> way because you don't have to double-check that 'point_struct'
    >> is really pointing to the structure you want to pass to the
    >> function.


    > Is there any situation where it would be advantagoues to use the first
    > option?.


    I guess you would use it when you have several structures and you
    select which one of them to be passed to the function only at run
    time.

    > My current situation is the function needs to change the contents of the
    > structure. The structure actually contains a linked list of integers and
    > float that is dynamically allocated by the called function.
    > I am just trying to avoid making the function global.


    I guess you mean the structure, not the function. And in such cases
    it would be reasonable to pass a pointer to that structure to the
    function - that's exactly one of the things pointers are good for.
    Actually, you won't see it very often that a structure is passed
    directly to a function due to the extra time required to copy it
    on function invocation, passing a structure by address is done much
    more often.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 8, 2004
    #6
  7. In article <>,
    -berlin.de wrote:
    > Fernan Bolando <> wrote:
    >> In article <>,
    >> -berlin.de wrote:

    > I guess you mean the structure, not the function. And in such cases
    > it would be reasonable to pass a pointer to that structure to the
    > function - that's exactly one of the things pointers are good for.
    > Actually, you won't see it very often that a structure is passed
    > directly to a function due to the extra time required to copy it
    > on function invocation, passing a structure by address is done much
    > more often.


    thanks very much


    ,Fernan

    --
    Public PGP/GnuPG key (http://www.fernski.com)
    pub 1024D/681A5498 2003-10-26 Fernan Bolando
    Key fingerprint = BAC8 EA6A CA98 26F3 3DB4 74D1 988E 56F7 681A 5498
    Fernan Bolando, Aug 8, 2004
    #7
    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. Chris Fogelklou
    Replies:
    36
    Views:
    1,335
    Chris Fogelklou
    Apr 20, 2004
  2. Ole
    Replies:
    4
    Views:
    585
    Michael Wojcik
    Oct 26, 2004
  3. beetle
    Replies:
    2
    Views:
    874
    beetle
    Jan 25, 2005
  4. Zero
    Replies:
    16
    Views:
    633
    Barry Schwarz
    Nov 19, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    443
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page