Search code examples
cfunctionstructfunction-pointerstypedef

Access data member from function pointer in struct


I gotta admit. I lack experience with C and therefore I am unsure how to implement a function for a struct. There are plenty of answers out there: Define functions in structs, Can I define a function inside a C structure?, C - function inside struct, but they don't answer the result I am looking for. Now what I want is the following:

Imagine I have a struct like this:

typedef struct Ball {
  float x, y;
  float speedX, speedY;
  float radius;
  void (*Draw)();
} Ball;

Now I want the Draw fuction to be able to access the members of the Ball variable, as if I had an instance of a struct in C++. The function should be able to access the variables and modify them as much as I want it to. Is this even possible?

I tried some ridiculous stuff like this, but that didn't lead anywhere.

typedef struct Ball {
  float x, y;
  float speedX, speedY;
  float radius;
  void (*Draw)();
} Ball;

void Draw(float *x, float *y, float *speedX, float *speedY, float *radius) {
  DrawCircle((int)*x, (int)*y, (int)*radius, WHITE);
}

Here the C++ equivilant:

    struct Ball {
        float x{}, y{};
        float speedX{}, speedY{};
        float radius{};
        void Draw() {
            DrawCircle((int)x, (int)y, (int)radius, WHITE); //I know I could static_cast, but who cares :)
        }
    };
int main(int argc, char ** argv) {
    Ball ball;
    ball.x = 100.f;
   ...
    ball.Draw();
}

As you can see, the C++-way is pretty darn simple, I just couldn't figure it out for C.


Solution

  • Just declare the pointer to function the following way

    typedef struct Ball {
      float x, y;
      float speedX, speedY;
      float radius;
      void (*Draw)( struct Ball * );
    } Ball;
    

    When the function will be called pass to it a pointer to an object of the structure type.

    For example

    Ball ball = { /* initializers of data members of the structure */ };
    
    ball.Draw( &ball );
    

    The function can be implemented for example like

    void Draw( struct Ball *ball ) 
    {
        DrawCircle( ball->x, ball->y, ball->radius, WHITE );
    }
    

    and the data member Draw of an object of the structure type can be assigned like

    ball.Draw = Draw;
    

    Or you could declare the function DrawCircle like

    void DrawCircle( struct Ball *ball );
    

    provided that it is used only for objects of the type struct Ball.

    And directly initialize the data member Draw with this function

    ball.Draw = DrawCircle;