You are not logged in.

#1 2006-09-18 18:16:22

cmp
Member
Registered: 2005-01-03
Posts: 350

Setting global struct in C

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

#2 2006-09-18 18:27:28

nogoma
Member
From: Cranston, RI
Registered: 2006-03-01
Posts: 217

Re: Setting global struct in C

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

#3 2006-09-18 18:33:22

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: Setting global struct in C

thanks. it's really been a long time.
to my defense in python there is almost no difference between " and '.

Offline

#4 2006-09-18 19:08:53

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Setting global struct in C

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

#5 2006-09-18 22:03:22

bboozzoo
Member
From: Poland
Registered: 2006-08-01
Posts: 125

Re: Setting global struct in C

phrakture wrote:

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

#6 2006-09-18 22:14:57

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Setting global struct in C

bboozzoo wrote:

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

Board footer

Powered by FluxBB