Sign in to follow this  
googlyeyes

Converting C++ to C

Recommended Posts

I need to get my code compiling in C, I don't use any classes of C++ specific features (to my knowledge) so it should be fairly easy. But im running into all sorts of weird errors! help! image.h
#ifndef IMAGE_H
#define IMAGE_H

enum imagetype
{
	NONE   = 0,
	RGB24  = 1,
	RGBA32 = 2
};

struct imagenode
{
	unsigned int id;
	imagetype format;
	int width;
	int height;
	void* data;
	imagenode* next;
};

bool addimage(unsigned char id, imagetype format, int width, int height, void* data);
bool removeimage(unsigned char id);
void testloadimage();

#endif /*IMAGE_H*/

test.c
#include "image.h"

gcc test.c
In file included from test.c:1:
image.h:14: error: parse error before "imagetype"
image.h:14: warning: no semicolon at end of struct or union
image.h:18: error: parse error before '*' token
image.h:18: warning: data definition has no type or storage class
image.h:19: error: parse error before '}' token
image.h:21: error: parse error before "addimage"
image.h:21: error: parse error before "imagetype"
image.h:21: warning: data definition has no type or storage class
image.h:22: error: parse error before "removeimage"
image.h:22: warning: data definition has no type or storage class

c++ mode works fine for this example. thanks in advance

Share this post


Link to post
Share on other sites
Well the first problem I see is that there is no set bool variable in standard c, you would need to define one for yourself, the other problem is I think that enum might be handled differently but I can't remember its been a while since I've coded in pure c.

*edit* someone beat me to it

Share this post


Link to post
Share on other sites
Enums works just fine in C, except for slightly relaxed conversion rules and no scoping.
You need to either typedef the type names or prefix them with enum or struct.

So either:
typedef enum {
NONE = 0,
RGB24 = 1,
RGBA32 = 2
} imagetype;
Or:
struct imagenode {
unsigned int id;
enum imagetype format;
int width;
int height;
void* data;
struct imagenode* next;
};
You'll have to define a boolean type yourself or perhaps use C99's <stdbool.h>

Share this post


Link to post
Share on other sites
okay hopefully the last problem

one file has

86: imagenode* list = imagelist;
87: imagenode* deleteme = list->next;
88: list->next = list->next->next;

the imagenode struct is
typedef struct
{
unsigned int id;
imagetype format;
int width;
int height;
void* data;
struct imagenode* next;
} imagenode;

imagelist is a global
imagenode* imagelist;

the compiler says:
init.c:87: warning: initialization from incompatible pointer type
init.c:88: error: dereferencing pointer to incomplete type

Share this post


Link to post
Share on other sites
You need to write:
typedef struct imagenode_t {
unsigned int id;
imagetype format;
int width;
int height;
void* data;
struct imagenode_t *next;
} imagenode;
Or even:
typedef struct imagenode_t imagenode;

struct imagenode_t {
unsigned int id;
imagetype format;
int width;
int height;
void* data;
imagenode *next;
};
Since the compiler can't determine that there's an imagenode type in advance.
You could drop the added _t too but then you'd break C++ compability.

Share this post


Link to post
Share on other sites
Just want to mention that there is, in fact, a boolean type in standard C. Its called "_Bool" and, if you include <stdbool.h>, it can even be referred to as "bool".

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