reference expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pietro Cerutti

    #1

    reference expression

    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_r eference_a_rule >)
    {
    if(<something_t o_reference_a_r ule)
    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!

    --
    Pietro Cerutti

    PGP Public Key:

  • Pietro Cerutti

    #2
    Re: reference expression

    Pietro Cerutti wrote:
    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)
    >
    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_r eference_a_rule >)
    {
    if(<something_t o_reference_a_r ule)
    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!
    >

    --
    Pietro Cerutti

    PGP Public Key:

    Comment

    • Ben Bacarisse

      #3
      Re: reference expression

      Pietro Cerutti <gahr_AT_gahr_D OT_ch_DO_NOT_SP AMwrites:
      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_r eference_a_rule >)
      {
      if(<something_t o_reference_a_r ule)
      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.

      --
      Ben.

      Comment

      • Thad Smith

        #4
        Re: reference expression

        Ben Bacarisse wrote:
        Pietro Cerutti <gahr_AT_gahr_D OT_ch_DO_NOT_SP AMwrites:
        >
        >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_r eference_a_rule >)
        >{
        > if(<something_t o_reference_a_r ule)
        > 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);
        }
        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.

        --
        Thad

        Comment

        Working...