C programming question ....

Susan Macchia susan
Wed Nov 8 11:16:30 PST 2006



--- Ben Duncan <bns at meta3.net> wrote:

> Ok, wrote this lil' ole snippet of code to test a theory ...
> -------------------------------------------------------------------
> 
> typedef void* my_pointer ;
> 
> struct rlib;
> typedef struct rlib rlib;
> typedef void* my_pointer ;
> 
> struct rlib;
> typedef struct rlib rlib;
> 
> struct rlib_signal_functions {
>          int (*signal_function)(rlib *, my_pointer);
>          my_pointer data;
> };
> 
> struct rlib_metadata {
>          int rlib_value ;
>          int rval_formula ;
>          char rlib_from_xml [80] ;
>          char xml_formula [80] ;
>          char rlib_pcode [120] ;
>          char *formula_code ;
> };
> 
> struct rlib {
>          int current_page_number;
>          int current_line_number;
>          int detail_line_count;
>          int start_of_new_report;
> 
>          int font_point;
> 
>          int current_font_point;
> 
>          char *output_encoder_name;
> 
> };
> 
> int main ( int Argc, char **Argv )
> 
> struct rlib_signal_functions {
>          int (*signal_function)(rlib *, my_pointer);
>          my_pointer data;
> };
> 
> struct rlib_metadata {
>          int rlib_value ;
>          int rval_formula ;
>          char rlib_from_xml [80] ;
>          char xml_formula [80] ;
>          char rlib_pcode [120] ;
>          char *formula_code ;
> };
> 
> struct rlib {
>          int current_page_number;
>          int current_line_number;
>          int detail_line_count;
>          int start_of_new_report;
> 
>          int font_point;
> 
>          int current_font_point;
> 
>          char *output_encoder_name;
> 
> };
> 
> int main ( int Argc, char **Argv )
> 
> ......
> ...................
> -------------------------------------------------------
> 
> And it COMPILES and works. SO, it seems that
> 
> struct rlib;
> typedef struct rlib rlib;
> 
> Is a way to prototype a Structure ...
> 
> Ok, my questions to the REAL C guru's out there is:
> Why would you want to do such a thing when simply moving
> the ACTUAL structure ahead of things would solve having to
> create extra lines of code protyping the structure ?
> 
> Thanks ...
> 
Don't know if you'd consider me a guru, but I've been programming in C/C++
since 1985, so...

In this case, I would have put the structure ahead and avoided the extra lines
and the forward reference (as you suggest).  The only time I have used forward
references like this is when there is an interdependency between the 2.  Like
Foo has a pointer to Bar and Bar has a pointer to Foo.

HTH,
Susan

_____________________________
Susan Macchia
mailto:susan at smacchia.net
http://www.smacchia.net
_____________________________



More information about the Linux-users mailing list