[vox-tech] C preprocessor - "soft reference"?

Micah Cowan micah at cowan.name
Thu Jan 18 13:20:27 PST 2007


Absolutely! See below :)

On Thu, 2007-01-18 at 15:32 -0500, Peter Jay Salzman wrote:
> The variable p and the variable's name "p" need to be passed to the macro
> function to test for nullness and then to print some message involving the
> variable's name.
> 
> Is there a crafty way of doing this so that only one thing needs to be
> passed?
> 
> Thanks!
> 
> 
> 
> 
> 
> #include <stdlib.h>
> #include <stdio.h>
> 
> #define CHECK_FOR_NULLITITY(var,varname) \
> 	if ( (var) == NULL ) \
> 	{ \
> 		printf("%s is null.\n", varname); \
> 		exit(EXIT_FAILURE); \
> 	}
> 

#define CHECK_FOR_NULLITITY(var) \
	if ( (var) == NULL ) \
	{ \
		printf("%s is null.\n", #var); \
		exit(EXIT_FAILURE); \
	}

> 
> 
> int main( void )
> {
> 	char *p = NULL;
> 	CHECK_FOR_NULLITITY( p, "p" );
> 
> 	return 0;
> }
> _______________________________________________
> vox-tech mailing list
> vox-tech at lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech
-- 
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/



More information about the vox-tech mailing list