Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 968
Noname Avatar asked Oct 15 '25 19:10

Noname


1 Answers

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;
like image 93
Vlad from Moscow Avatar answered Oct 18 '25 12:10

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!