Let me explain the situation : I have a C struct as follows :
typedef struct {
int *val;
char *name;
} tStruct;
This structure might be populated as follows : - val can be null if the "val" value is not available, otherwise val is an integer value (can be negative) - name can be an empty string if name is not available, or a filled string (not null pointer here) if a name is available.
I wish to write a log line as follows :
- if val is invalid, name is valid (equals WOOT):
LOG val=# name=WOOT
- if val is invalid, name is invalid :
LOG val=# name=#
- if val is valid, name is invalid:
LOG val=123456 name=#
- if val is valid, name is valid (equals WOOT):
LOG val=123456 name=WOOT
This means I'd need to use either printf("val=%s name=%s",...) or printf("val=%d name=%s",...) depending on the id value (so that I can either output a # or the integer). Outputing a fake integer value when val is invalid is not suitable since any signed or unsigned value is possible.
Any idea ? I wish I could avoid the following kind of construct because my struct will actually contain many fields, making too many the "if" combinations :
if ( (struct.val == NULL ) && ( struct.name ) ) then printf ("val=# name=%");
else if ((struct.val == NULL ) && ( ! struct.name ) ) then printf ("val=# name=#");
else if ...
Thank you