[nzlug] C expert advise wanted

Raimund Eimann raimund at cs.auckland.ac.nz
Thu Mar 29 15:07:48 NZST 2007


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.

int proc_a(var *c, struct struct_a **a) {
	struct struct_a *dummy = malloc(...);
	
	dummy->a = ...
	dummy->b = ...
	...
	*a = dummy;
}

int proc_b(var *c, struct struct_b **b) {
	struct struct_b *dummy = malloc(...);
	
	dummy->a = ...
	dummy->b = ...
	...
	*b = dummy;
}

Is there some way of avoiding this code repetition and only have a single 
function which is capable of processing both types of structs?

Cheers,
Raimund



More information about the NZLUG mailing list