Jump to content
  • Advertisement
Sign in to follow this  
BlackDuck

struct and function pointers

This topic is 3031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Again, I have a project that can only be done in C. So I am told I can create struct with functions inside and call it similiar to biPlane->IncreaseSpeed(2); My understanding is each file in a project represent something that is similar like player, enemy, input, display etc. So I will have a file call plane.h In there do I create a struct
struct plane
{
    Vector3D loc;
    float speed;
}

I wish to understand, do I need a implementation file and a header file for plane. Where do I put the IncreaseSpeed function How do I create the plane object like
plane biPlane = new plane();

And then increase the speed like
biPlane->IncreaseSpeed(2);

And probably before all this, initialise the speed to zero

Share this post


Link to post
Share on other sites
Advertisement
Quote:

I wish to understand, do I need a implementation file and a header file for plane


Sure, if you wish.

HINT:
biPlane.IncreaseSpeed(2);

Is synthetic sugar for:
Plane::IncreaseSpeed( &biPlane, 2 );

Share this post


Link to post
Share on other sites
Please review code. Please correct me, but I believe I have create a struct that contains a member and three methods..kind of...class speaking ;) maybe?


#include <stdio.h>

struct plane
{
float m_speed;

void IncreaseSpeed (void)
{
m_speed++;
}

void SetSpeed (float speed)
{
m_speed = speed;
}

void Initialise(void)
{
m_speed = 0;
}
};

int main (void)
{
plane biPlane;

biPlane.Initialise();
biPlane.IncreaseSpeed();

printf("m_speed: %f\n", biPlane.m_speed);

biPlane.SetSpeed(5.0);

printf("m_speed: %f\n", biPlane.m_speed);

return 0;
}



Now any advice how to change this so I can do this below using function pointers. Do I have it all wrong?


biPlane->Initialise();
biPlane->IncreaseSpeed();
biPlane->SetSpeed(5.0);

Share this post


Link to post
Share on other sites
Function pointers in structs in C are used more for hacking polymorphic behaviour. Unless that is required here, member functions are far simpler to implement. Code below is deliberately a bit cryptic because this sounds a bit like homework.


struct type
{
int value;
};

void t_constructor(struct type *t,int value)
{
t->value=value;
}

void t_method(struct type *t,int add)
{
t->value+=add;
}

void f()
{
type t;
t_constructor(&t,10);

t_method(&t,23);
}








would be a simple example of how to implement non-polymorphic member functions in C.

Polymorphic behaviour with function pointers in structs would look more like this:


struct type
{
int value;

void (*method)(struct type*,int);
};

void t_method_one(struct type *t,int add)
{
t->value+=add;
}

void t_method_two(struct type *t,int sub)
{
t->value-=sub;
}

void t_add_constructor(struct type *t,int value)
{
t->value=value;
t->method=t_method_one;
}

void t_sub_constructor(struct type *t,int value)
{
t->value=value;
t->method=t_method_two;
}

void poly(struct type *t)
{
(*t->method)(t,23); // think this is correct syntax, can't remember
}

void f()
{
struct type a,b;

t_add_constructor(&a,10);
t_sub_constructor(&b,20);

poly(&a);
poly(&b);
}








As _fastcall points out above, this is all really just sort of manually doing what a C++ compiler is most likely doing under the hood.

Share this post


Link to post
Share on other sites
Quote:
Original post by BlackDuck
I have a project that can only be done in C. ...
Quote:
Original post by BlackDuck
Please review code. Please correct me, but I believe I have create a struct that contains a member and three methods..kind of...class speaking ;) maybe? ...

Maybe I've overlooked something, but it seems me that C and C++ is confused here. The OP seems me to ask for a C solution to mimik some sort of C++ functionality. BlackDuck, please clarify whether just pure C or also C++ is an option for your project.

Share this post


Link to post
Share on other sites
Pure C..

My tutor mention that I could setup a struct with function pointers to make it behaviour similiar to OO but there is no real need but to make it readable and cleaner.

The project is to make a 3D program to simulate a force (gravity), 3D animation, interaction, some other stuff so struct / function pointers as such is not access but it would make my code more readable so that is why I am asking. i don't mind if the responses are cryptic, as long as it makes sense and it is the direction I wish to implement. At the end the tutor did say that he may have confused things, it has been a while he said. But he mention that I am to put all relate functions and attributes of my plane in a file for itself (maybe a header) inside a struct since I can have functions there too.

Now I only seem to be able to call the function like foo.bar rather than foo->bar

Is there a way?

At the end the goal is to make the code readable and to keep things inside a struct to make it neater.

Share this post


Link to post
Share on other sites
It's not clear what you're really looking for. There is no way to simulate the C++-style OOP syntax you want in C.

If you want readable code, don't use C (or C++). C is a 40 year old programming language (C++, 30 year old) that was designed during a time when assembly code was seen to be the only legitimate way to program a computer. Don't expect it to be elegant or "clean".

That being said, why can't you simply do it the way a C programmer would do it. Leave your C++ habits with C++ and adopt a C mentality:


struct plane
{
Vector3D loc;
float speed;
}

void planeNew(plane* p)
{
// do your initialization here
p->speed = 0;
p->loc.x = 0;
p->loc.y = 0;
p->loc.z = 0;
}

void planeIncreaseSpeed(plane* p, float amount)
{
p->speed += amount;
}

...

plane players[NUM_PLAYERS];

...

void setup()
{
for (i = 0; i < NUM_PLAYERS; i++)
{
planeNew(&players);
}
}

...

void onSpeedUp(int playerId)
{
planeIncreaseSpeed(&players[playerId], 2);
}

Share this post


Link to post
Share on other sites
I have to disagree about C++ habits in C stuff.

It is in fact useful to have function pointer members in a struct: think about a GUI structure. Drawing/function/whatever are just pointers, so the GUI will be flexible, and not the immediate mode stuff

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
It is in fact useful to have function pointer members in a struct: think about a GUI structure. Drawing/function/whatever are just pointers, so the GUI will be flexible, and not the immediate mode stuff


There's nothing wrong with using function pointers in your structs. If you need polymorphic behavior, function pointers are the correct way to do it.

But the OP isn't looking for polymorphism. He's looking to group state and behavior in his code and a way to pretty up the syntax. This isn't possible in C. (And if it is, it requires unnecessary abuse of the preprocessor).

Share this post


Link to post
Share on other sites
Quote:
Original post by BlackDuck
Now I only seem to be able to call the function like foo.bar rather than foo->bar

Is there a way?
I think it isn't really a question whether to use a dot or an arrow. Even in C++ you have both ways, dependend on whether you have a reference or else a pointer to the class instance. Instead, the exercise is just to declare a struct where function pointers are members, to set such pointers to an actual function, and to invoke it.

(A rule in GDnet is not to solve home work. So I suggest to use your search engine of choice and look for "c function pointer". That gives you a plenty of hits. Then put that into a struct. If it didn't compile or execute, then we like to help on source code / error messages / whatever you show us.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!