user3163916
user3163916

Reputation: 328

C Struct-Function Pointer-Passing Self Reference As Argument

I would like to run the function pointed by my struct with auto-filling functionality.

This is the part that I'm working on:

#include <stdio.h>
#include <stdlib.h>

struct Fra { //Fraction
  int n;     //Numerator
  int d;     //Denominator
  void (*p)(struct Fra*);
  void (*sD)(int, struct Fra*);
  void (*sN)(int, struct Fra*);
};

void print(struct Fra*);
void setDenom(int, struct Fra*);
void setNum(int, struct Fra*);

int main() {

  struct Fra* fraA = 0;

  fraA = (struct Fra*) malloc(sizeof(struct Fra));

  fraA->sN = setNum;
  fraA->sN(2, fraA);
  fraA->sD = setDenom;
  fraA->sD(3, fraA);
  fraA->p = print;
  fraA->p(fraA);

  return 0;
}

And this is what I've been trying to achieve

From:

fraA->sN(2, fraA);
fraA->sD(3, fraA);
fraA->p(fraA);

To:

fraA->sN(2);
fraA->sD(3);
fraA->p();

After spending some time on trial-error, I've arrived to the conclusion that I need assistance on this. I've tried browsing, but it seems I don't have the right keyword, so I'm unable to verify whether this question is a double or not.

Thanks for any help.

Upvotes: 4

Views: 7752

Answers (3)

7ffd
7ffd

Reputation: 1

There is an example that works for me:

typedef struct _log_t
{
    log_level_t threshold;
    void (*logger)(struct _log_t*, log_level_t, char *, ...);
} 
log_t;

Upvotes: 0

glglgl
glglgl

Reputation: 91119

The only really useful way I can think of would be to provide functions which do the job:

void call_print(struct Fra* fra)
{
    fra->p(fra);
}

void call_setDenom(int val, struct Fra* fra)
{
    fra->sD(val, fra);
}

void call_setNum(int val, struct Fra* fra);
{
    fra->sN(val, fra);
}

and use these:

call_setNum(2, fraA);
call_setDenom(3, fraA);
call_print(fraA);

Upvotes: 1

alk
alk

Reputation: 70971

You could declare some macros to savely always pass the correct reference, that's all you can do:

#define FRA_NEW(this, sN sD, sP) \
{ \
  (this) = calloc(sizeof(*(this))) \
  if (this) \
  { \
    (this)->sN = (sN); \
    (this)->sN = (sD); \
    (this)->sN = (sP); \
  } \
}

#define FR_DELETE(this) \
  free(this)

#define FRA_PRINT(this) \
  (this)->print(this)

#define FRA_SETNUM(this, num) \
  (this)->setNum(this, num)

#define FRA_SETDENOM(this, denom) \
  (this)->setDenom(this, denom)

Also I'd propose to have "this" always as first parameter to the "member"-functions.

int main(void) 
{
  struct Fra * fraA = NULL;

  FRA_NEW(fraA, setNum, setDenom, print);
  if (NULL == fraA)
  {
    perror("FRA_NEW() failed");
    return 1;
  }

  FRA_SETNUM(fraA, 2);
  FRA_SETDENOM(fraA, 3);
  FRA_PRINT(fraA);

  FRA_DELETE(fraA);

  return 0;
}

Upvotes: 2

Related Questions