How to work with structures in functions.

This is a discussion on How to work with structures in functions. within the C Programming forums, part of the General Programming Boards category; Hi All, My question is how You guys prefer to call structure body from function (in embedded systems)? Code: Ex1. ...

  1. #1
    Wymiatacz
    Join Date
    Jan 2007
    Location
    Far away
    Posts
    6

    Question How to work with structures in functions.

    Hi All,

    My question is how You guys prefer to call structure body from function (in embedded systems)?
    Code:
    Ex1.
    
        STR NewStr;
        .
        .
        fun1(&NewStr)
        .
        .
    
    void fun1(STR *Ptr)
    {
        .
        .
        Ptr->some_variable;
        .
        .
    }
    
    
    Ex2.
    
        STR NewStr;
        .
        .
        fun1()
        .
        .
    
    void fun1()
    {
        .
        .
        Ptr.some_variable;
        .
        .
    }
    What's the different in :
    - address stuff
    - speed

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Passing a pointer to a structure usually means less overhead (memory) than passing the entire structure itself. Also, if you don't pass a pointer to it, you can't make changes to it that keep once the function has returned.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Wymiatacz
    Join Date
    Jan 2007
    Location
    Far away
    Posts
    6

    Question Point to struct as an argument or as local point?

    Quote Originally Posted by quzah
    Passing a pointer to a structure usually means less overhead (memory) than passing the entire structure itself. Also, if you don't pass a pointer to it, you can't make changes to it that keep once the function has returned.


    Quzah.

    Hi Quazah,

    And what about stuff like that:
    Code:
    Ex1.
    
        STR NewStr;
        .
        .
        fun1(&NewStr)
        .
        .
    
    void fun1(STR *Ptr)
    {
        .
        .
        Ptr->some_variable;
        .
        .
    }
    
    
    Ex2.
    
        STR NewStr;
        .
        .
        fun1()
        .
        .
    
    void fun1()
    {
        STR *Ptr = NewStr;
        .
        Ptr->some_variable;
        .
        .
    }
    
    (STR_SPORT_COMMS_DATA *) &GsmData
    
    Ex3.
    
        STR NewStr;
        .
        .
        fun1()
        .
        .
    
    void fun1()
    {
        STR *Ptr = (STR *) &NewStr;
        .
        Ptr->some_variable;
        .
        .
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > My question is how You guys prefer to call structure body from function (in embedded systems)?
    Like this
    fun1(&NewStr);

    > What's the different in :
    >- address stuff
    >- speed
    None of any significance which is worth polluting the code with vast numbers of global variables.
    In addition to being a very minimal difference, it's also going to vary depending on your compiler settings, compiler and chosen machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. COntrol structures and functions questions
    By angelicscars in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 10:50 AM
  2. I cant get functions to work!
    By -Dan- in forum C++ Programming
    Replies: 5
    Last Post: 06-13-2004, 07:15 AM
  3. Structures and Functions
    By Extol in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 07:28 PM
  4. Array of Structures and Functions
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 07:06 AM
  5. functions won't work together
    By wjday in forum C Programming
    Replies: 6
    Last Post: 01-25-2002, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21