Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
21615 Discussions

Passing Structures into Functions

Altera_Forum
Honored Contributor II
1,797 Views

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?
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
912 Views

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.
0 Kudos
Altera_Forum
Honored Contributor II
912 Views

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

would this mean, that no instance is created by this code?
0 Kudos
Altera_Forum
Honored Contributor II
912 Views

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.
0 Kudos
Altera_Forum
Honored Contributor II
912 Views

Thanks to you both. I appreciate it.

0 Kudos
Reply