You are not logged in.
Pages: 1
I don't know what to call this, in eiffel it's called manifest notation: I want to create an array of structs the same time I reserve a variable for them. I've seen this in many places, but as some time has passed, since I coded in c, I don't remember the correct syntax. It should be something like this:
typedef struct{
char sprite;
bool walkable;
}FIELD;
const static FIELD field_table[] = {
{" ",1},
{"#",0}
};
I hope, that someone of you can help me.
Offline
If we're talking straight C (not C++), then bool should be int (there is no bool type in C), and you should use single quote marks instead of double quotes for characters:
typedef struct {
char sprite;
int walkable;
} FIELD;
const static FIELD field_table[] = {
{' ',1},
{'#',0}
};
"gcc -Wall" compiles that, no problem
-nogoma
---
Code Happy, Code Ruby!
http://www.last.fm/user/nogoma/
Offline
thanks. it's really been a long time.
to my defense in python there is almost no difference between " and '.
Offline
Side note, if you're using C99 (you should!), you can include <stdbool> and use bool/true/false.
Side note #2. There is no way, in C, to get the size of an array. You need to use a "trick" to know your size. Typically, when using code like you have, one does:
#include <stdbool>
typedef struct
{
char sprite;
bool walkable;
} FIELD;
const static FIELD field_table[] =
{
{' ', true},
{'#', false},
{0, false}
};
And then you loop until my_field.sprite == 0. Alternatively, you could add a "#define FIELD_TABLE_LENGTH 2" or something, but that adds potential errors if you were to add an entry at a later date and forget to change then length.
Offline
Side note, if you're using C99 (you should!), you can include <stdbool> and use bool/true/false.
Side note #2. There is no way, in C, to get the size of an array. You need to use a "trick" to know your size. Typically, when using code like you have, one does:
#include <stdbool> typedef struct { char sprite; bool walkable; } FIELD; const static FIELD field_table[] = { {' ', true}, {'#', false}, {0, false} };
And then you loop until my_field.sprite == 0. Alternatively, you could add a "#define FIELD_TABLE_LENGTH 2" or something, but that adds potential errors if you were to add an entry at a later date and forget to change then length.
stdbool is a nice hack where you have
#define true 1
#define false 0
as for the size of an array, since it's known at compile time, sizeof will do
size_t s = sizeof(field_table) / sizeof(FIELD);
and since we're targeting C99, then proper initialisation of structs looks like this:
FIELD a = {.sprite = '#', .walkable = true };
Offline
stdbool is a nice hack where you have
#define true 1 #define false 0
That is an implementation detail. stdbool is part of ISO 9899:1999, thus it is standard. As the gcc implementation provides them as complie-time constants, there is no loss when using "true" and "false" in place of 1 and 0.
Offline
Pages: 1