Skip to main content
GameDev.net gamedev.net
🔒 Locked

Converting a C++ program to a C program

Started by tekmoto Oct 1, 2006 at 7:50 PM 4 replies 1.5k views
Original Post
tekmoto
tekmoto
Hi everyone. I hope the weekend treated everyone well. I am in the painful process of converting one of my programs written in C++ over to C. I am aware of some of the major differences between the two, but I'm afraid I might be missing some specifics. Are constructors / functions for structs allowed in C? E.g., struct Data { int a; Data(int i) { a = i; } int Transfer() { return a; } }; I imagine this wouldn't work, since the "new" operator isn't present in C (right?).
Beem
Beem
No. C is not an object oriented programming language. You'll have to use handles and functions instead of classes and methods if you want to program using objects. If you need an example let me know. Also note that when declaring a struct you should declare it in like manner:

typedef struct tagData
{
...
}Data;

Otherwise everytime you declare and instance of the structure you would have to type "struct Data" instead of just "Data".
tekmoto
tekmoto
So this is what I should have instead:

/// Code begins

typedef struct tagData
{
int a;
}Data;

void Initialize(Data* data, int i)
{
data->a = i;
}

int TransferData(Data* data)
{
return data->a;
}

/// Code ends


Is that what you mean?
nullsquared
nullsquared
Quote:
Original post by tekmoto
So this is what I should have instead:

/// Code begins

typedef struct tagData
{
int a;
}Data;

void Initialize(Data* data, int i)
{
data->a = i;
}

int TransferData(Data* data)
{
return data->a;
}

/// Code ends


Is that what you mean?


I would have something like this:
typedef struct _tagPoint {    float x, y, z;    void (*_con)(struct _tagPoint *this, float x, float y, float z);    void (*_des)(struct _tagPoint *this);    // example function    void (*toZero)(struct _tagPoint *this);} point;void _conPoint(point *this, float x, float y, float z) {    this->x = x;    this->y = y;    this->z = z;}void _desPoint(point *this) {    // nothing}void _toZeroPoint(point *this) {    this->x = this->y = this->z = 0;}// __VA_ARGS__#define DEFINE_NEW(t) t *_make##t(...)#define DEFINE_DELETE(t) void _delete##t(t *o)#define NEW(t, ...) _make##t(__VA_ARGS__)#define DELETE(t, o) _delete##t(o)DEFINE_NEW(point) {    point *p = (point*)malloc(sizeof point);    p->_con = &_conPoint;    p->_des = &_desPoint;    p->toZero = &_toZeroPoint;    // deal with the '...' operator, I forgot how and have no time to    // look it up right now    // assuming you retrieved the values from the '...'    p->_con(p, theX, theY, theZ);    return p;}DEFINE_DELETE(point) {    o->_des(o);    free(o);}int main() {    point *p =  NEW(point, 30, 25, 0);    p->toZero(p);    DELETE(p);}

Not compiled, but I *assume* it'll work. Ugly though.
XXX_Andrew_XXX
XXX_Andrew_XXX
Have you considered using SWIG to do this?

If performance isn't a problem SWIG can sometimes be the fastest way to do things like this. The only downside is you may need to write cleaned up headers or .i files for swig, but it's still probably less work
tekmoto
tekmoto
Thanks for everyone's responses! They are helpful.

I just did some research on SWIG, it looks pretty cool. I will look into it.

Thanks again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.