2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Noname
  • 69
  • 7
  • 2
    It probably is not a good idea to try to emulate C++ in C. In C you would normally just write a free function that takes the object you want to operate on by pointer as an argument. (Which is effectively exactly how non-static member functions work in C++. The explicit parameter in my C suggestion is the equivalent of `this` in C++.) – user17732522 May 31 '23 at 21:01
  • @user17732522 Ah ok. I didn't even think of that option. Great! – Noname May 31 '23 at 21:02

2 Answers2

3

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;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If all of your Ball structures share the same Draw function, then there is no need to store a function pointer to that function in the structure. This is a pointless waste of memory and complicates things needlessly.

Just have the information that is unique to each Ball stored in the structure:

typedef struct Ball {
  float x, y;
  float speedX, speedY;
  float radius;
  // void (*Draw)(); No need for this!
} Ball;

And make a function that draws a ball:

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

Then you can have:

int main(int argc, char ** argv) {
    Ball ball;
    ball.x = 100.f;
    ...
    draw_a_ball(&ball);
}
pmacfarlane
  • 3,057
  • 1
  • 7
  • 24