stian
stian

Reputation: 1987

how to have a generic operator variable?

this might be a newbie question and I see something similar asked before, but I hope someone will help me. Below is a small (hopefully) illustrative code. I want to have two functions share some code. They will both have a test, which boils down to: if (int operator int). It should be possible to describe the operator with a pointer, and then use what the pointer points to in the test, shouldnt it?

func1(){
  func3(1)
}

func2(){
  func3(2);
}

func3(int type){
  char *op;
  int value1;
  int value2;
  switch (type){
  case 1:
        op = ">";
        value1 = 1;
        value2 = 3;
        break;
  case 2: 
       op = "=";
       value1 = 2;
       value2 = 5;
       break; 
 }
 if (value1 *op value2){
     do something
 }
}

I find that > is converted to 62 (decimal) in a text to binary calculator, but this prints out 60 - why?

char *p = "<";
printf("%d\n", *p);

Upvotes: 2

Views: 92

Answers (4)

fredrik
fredrik

Reputation: 6638

You cannot do it exactly as you wrote it. But it should be doable with function pointers. Here is one way you might be able to do it:

func1(){
  func3(1)
}

func2(){
  func3(2);
}
typedef (int *myOperator)(int, int);
int operatorGt(int a, int b) {
  return a > b;
}

int operatorEq(int a, int b) {
  return a = b;
}


func3(int type){
  myOperator op = NULL;
  int value1;
  int value2;
  switch (type){
  case 1:
        op = &operatorGt;
        value1 = 1;
        value2 = 3;
        break;
  case 2: 
       op = &operatorEq;
       value1 = 2;
       value2 = 5;
       break; 
 }
 if (NULL != op && op(value1, value2)){
     do something
 }
}

I'm not giving any guarantees that this will compile or work - but the method is more likely to work. I have not compiled it - so there might be some compiler errors and/or warnings to fix for you. Wouldn't want to make it too easy :D

Upvotes: 1

user4815162342
user4815162342

Reputation: 155660

As others said, you cannot take the address of a C operator directly, but you can take the address of a function that only serves to use the operator. Since the number of operators is limited, you can — sort-of — convert an operator to a pointer like this:

#define DECLARE_OPFN(name, operator) bool op_##name(int op1, int op2) { \
  return op1 operator op2; \
}

DECLARE_OPFN(lt, <)
DECLARE_OPFN(le, <=)
DECLARE_OPFN(gt, >)
DECLARE_OPFN(ge, >=)
DECLARE_OPFN(eq, ==)
DECLARE_OPFN(ne, !=)

typedef bool (*opfn_type)(int, int);

opfn_type get_opfn(char *op)
{
  static const struct {
    const char *str;
    bool (*op)(int, int);
  } all_ops[]  = { {"<", op_lt}, {"<=", op_le}, {">", op_gt}, {">=", op_ge},
                   {"==", op_eq}, {"!=", op_ne} };
  int i;
  for (i = 0; all_ops[i].str; i++)
    if (!strcmp(op, all_ops[i].str))
      return all_ops[i].op;
  return NULL;
}

// ...
get_opfn(my_op_str)(val1, val2);

Upvotes: 1

Jason
Jason

Reputation: 32538

You actually want to be working with function pointers ... you can't simply use a char to become a function because the char value is equivalent to the token of an operator. Token substitution like you're attempting can only be done with the pre-processor.

Upvotes: 1

Barath Ravikumar
Barath Ravikumar

Reputation: 5836

It should be possible to describe the operator with a pointer, and then use what the pointer points to in the test, shouldnt it?

No am afraid , it's not possible to reference/dereference an operator using a pointer , because an operator is entirely different from a datatype.

Upvotes: 1

Related Questions