- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you very much, i now understand except for one thing,
would this mean, that no instance is created by this code?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks to you both. I appreciate it.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page