Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

Passing Structures into Functions

Hi Guys,

question

I need some clarification on passing structures into functions.

in what i'll outline below, i don't understand why the function prototypes are as they are.

This is how i understand it, but if i say something wrong please correct me.

example that confuses me

What i've got is the Niche stack with the below structure.

typedef
 
struct SSS_SOCKET { 
enum { READY, COMPLETE, CLOSE } state; 
int fd; 
int close; 
INT8U rx_buffer; 
INT8U *rx_rd_pos; /* position we've read up to */
INT8U *rx_wr_pos; /* position we've written up to */
 } SSSConn;

This says to me "define a structure type SSS_Socket, and create an instance called SSSConn.".

Now functions that use this have their prototypes as:

void SSS_handle_receive(SSSConn* conn)

and function call appears as:

SSS_handle_receive(&conn)

What i don't understand is why is an instance of a structure being used instead of the type itself i would have thought a prototype would be:

void SSS_handle_receive(SSS_SOCKET* conn)

can someone please explain whats happening here?

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    There is a typedef at the very beginning of the code making SSSConn visible as a type (alias 'struct SSS_SOCKET').

    If you reformat the source a bit it becomes beter visible

    
    typedef  struct SSS_SOCKET { 
        enum { READY, COMPLETE, CLOSE } state; 
        int fd; 
        int close; 
        INT8U rx_buffer; 
        INT8U *rx_rd_pos; /* position we've read up to */
        INT8U *rx_wr_pos; /* position we've written up to */
        } SSSConn;

    Most often we first see the declaration of the structure, followed by the typedef statement.

    
    struct SSS_SOCKET { 
        enum { READY, COMPLETE, CLOSE } state; 
        int fd; 
        int close; 
        INT8U rx_buffer; 
        INT8U *rx_rd_pos; /* position we've read up to */
        INT8U *rx_wr_pos; /* position we've written up to */
        } ;
    typedef struct SSS_SOCKET SSSConn ;
    

    The first code snippet does the two in one go.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you very much, i now understand except for one thing,

    would this mean, that no instance is created by this code?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You are correct, both forms only create a 'type'. You can exemplify this by slightly changing the first form into

    typedef  struct { 
        enum { READY, COMPLETE, CLOSE } state; 
        int fd; 
        int close; 
        INT8U rx_buffer; 
        INT8U *rx_rd_pos; /* position we've read up to */
        INT8U *rx_wr_pos; /* position we've written up to */
        } SSSConn;

    You see that you can leave out the ' SSS_SOCKET' tag.

    Only when you instantiate a 'type' actual storage will be allocated. e.g.:

    
    SSSConn master , slaves ;
    will actually reserve storage.