[nzlug] C expert advise wanted

Peter Butler peter.butler at 141.com
Thu Mar 29 16:01:18 NZST 2007


I'm not a C expert, but here's a suggestion.  You could just use one 
struct (struct_b) and leave the field "c" unpopulated for the structs 
that are supposed to be struct_a.  A bit inelegant and wasteful of 
memory, but programming often requires a choice between two distasteful 
options.

You could also declare proc to take void pointers and cast them to the 
appropriate type, although this requires passing in a flag to tell proc 
what type it's dealing with.  Equally messy.

I'm interested to see what other solutions people come up with.

Cheers

Peter

Raimund Eimann wrote:
> Hi,
>
> a C program I'm currently writing uses two very similar structs, say struct_a 
> and struct_b.
>
> struct struct_a {
> 	var a;
> 	var b;
> 	struct struct_a *next;	
> }
>
> struct struct_b {
> 	var a;
> 	var b;
> 	struct struct_a *c;	
> 	struct struct_b *next;
> }
>
> In order to process these structs, I've got two functions proc_a() and 
> proc_b(). Both function only operate on the (simple) variables a and b which 
> exist in both struct and are of the same type.
>   



More information about the NZLUG mailing list