Useful macro...or dangerous??

Kjartan Pier Emilsson Jardedlisfraedi kjartan at raunvis.UUCP
Wed Apr 27 00:42:19 AEST 1988


Hello world!
  
  This sentence is meant as an introducing one.  

  In my work I have often felt the need to test the equality of
two structures, for example test the equality of a Color structure
and so on. As I quickly became tired of gigantic IF tests, I decided
to build some sort of equality macro, and came down on the following
solution which seems to work.  (Of course if some members of a
structure are pointers, then the equal() function returns 0 if the 
pointers do not point to the same adress).  Now I would very much like
to know whether this is a foolproof way to test equality of structures,
or is there a hidden nasty little fellow who eludes me.

#define EQ(A,B) equal(A,B,sizeof(*(A)))

typedef struct {
	...
	...
	}ANYSTRUCTURE;

main(){
ANYSTRUCTURE some,thing,or,the,*other
...
...
if( EQ(&some,other) )
	printf("yupyup\n");
else if( EQ(&thing,&or) )
	printf("spulft.\n");
...
...
etc.
}

equal(a,b,size)
char *a,*b;
long int size;
{
while(*(a+si-1)== *(b+si-1) && si>0)
	si--;
if(si==0)
	return(1);
else
	return(0);
}

I herewith submit my possible blunders to the Blowtorches of the Net.

		Kjartan Pierre Emilsson, Reykjavik, ICELAND



More information about the Comp.lang.c mailing list