Sign in to follow this  
tekmoto

Converting a C++ program to a C program

Recommended Posts

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?).

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this