reference expression

P

Pietro Cerutti

Hi group,
is there any way in C to reference an expression in a variable?

I have a function matching the contents of a struct against a rule, and
I would like to pass the rule to be applied as an argument.

Assume the following structure:

typedef struct {
int field1;
int field2;
} structure;

and the following set of rules:

#define RULE1 (s->field1 = 1 && s->field2 = 2)
#define RULE2 (s->field1 = 2 && s->field2 = 3)

etc.

I would like to define the matching function as:

/*
* return 1 if s matches the rule, 0 otherwise
*/
int match(structure *s, <something_to_reference_a_rule>)
{
if(<something_to_reference_a_rule)
return (1);
else
return (0);
}

and the call the function as follows:

----8-->-------------------
structure s;

/* here s is filled somehow */

if(match(&s, RULE1))
/* rule 1 matches */

else if(match(&s, RULE2))
/* rule 2 matches */

----8-->-------------------


Any way to do something semantically equal to what explained above?

Thanks a lot!
 
P

Pietro Cerutti

Pietro said:
Hi group,
is there any way in C to reference an expression in a variable?

I have a function matching the contents of a struct against a rule, and
I would like to pass the rule to be applied as an argument.

Assume the following structure:

typedef struct {
int field1;
int field2;
} structure;

and the following set of rules:

#define RULE1 (s->field1 = 1 && s->field2 = 2)
#define RULE2 (s->field1 = 2 && s->field2 = 3)

clearly, this would have been:
#define RULE1 (s->field1 == 1 && s->field2 == 2)
#define RULE2 (s->field1 == 2 && s->field2 == 3)
 
B

Ben Bacarisse

Pietro Cerutti said:
Hi group,
is there any way in C to reference an expression in a variable?

I have a function matching the contents of a struct against a rule, and
I would like to pass the rule to be applied as an argument.

Assume the following structure:

typedef struct {
int field1;
int field2;
} structure;

and the following set of rules:

#define RULE1 (s->field1 = 1 && s->field2 = 2)
#define RULE2 (s->field1 = 2 && s->field2 = 3)

etc.

I would like to define the matching function as:

/*
* return 1 if s matches the rule, 0 otherwise
*/
int match(structure *s, <something_to_reference_a_rule>)
{
if(<something_to_reference_a_rule)
return (1);
else
return (0);
}

The closes thing is a pointer to a function. In you case it looks
like the function will take a structure * and return an int. I'd
write it like this for clarity:

typedef int rule(structure *);

int match(structure *s, rule *r)
{
return r(s);
}

In this case, match is almost pointless. Functions like:

int and(structure *s, rule *r1, rule *r2)
{
return r1(s) && r2(s);
}

are more interesting.
 
T

Thad Smith

Ben said:
The closes thing is a pointer to a function. In you case it looks
like the function will take a structure * and return an int. I'd
write it like this for clarity:

typedef int rule(structure *);

int match(structure *s, rule *r)
{
return r(s);
}

I agree.

Building on that structure the OP could define

int Rule1(structure *s){return (s->field1 == 1 && s->field2 == 2);}
int Rule2(structure *s){return (s->field1 == 2 && s->field2 == 3);}
....

As a side note, I would leave the parentheses on the return expression
to emphasize the actual rule.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top