0

Is there a way to declare a structure with default initalisation values?

I have a header file which defines a structur like this:

typedef struct struc_s
{
    size_t cost const = 2000;
    size_t dmg const = 100;
    size_t def const = 100;
    size_t hull const = 1500;
    size_t shield const = 300;
    size_t capacity const = 2;
    size_t destruc const = 10;
} struc_t;

But this ofcourse doesn't work.

I would also be fine with a way of declaring a var of type struc_t in this header file. But as I remember right. I would have to decalre it in the c file as extern

What I want to do is every where where this header is included i want to be able to do var = struc_s.dmg and and the result should be that var holds the value 100. But I dont want to declare struc_s anywhere else then in the header. Is there a way to archive this behavior?

dhein
  • 6,431
  • 4
  • 42
  • 74

2 Answers2

2

Not in the way you want.

When you do a typedef, you're defining the shape of a memory region, a process distinct from allocating and filling it.

A possible alternative:

typedef struct 
{
    size_t cost;
    size_t dmg;
    size_t def;
    size_t hull;
    size_t shield;
    size_t capacity;
    size_t destruc;
} struc_t;


#ifndef DEFAULT_STRUC_VALUES_DEFINED
#define DEFAULT_STRUC_VALUES_DEFINED 

const struc_t DEFAULT_STRUC = {
    .cost = 2000,
    .dmg = 100,
    .def = 100,
    .hull = 1500,
    .shield = 300,
    .capacity = 2,
    .destruc = 10
};
#endif

and then when you want to create a new one:

struc_t *new_struc = malloc(sizeof(struc_t));
memcpy(new_struc, DEFAULT_STRUC, sizeof(struc_t));

As a sidenote, is there a reason you're using size_t for your structure members? There's nothing inherently wrong with it, but it may change from platform to platform.

Christian Ternus
  • 8,406
  • 24
  • 39
  • would be default struct be accesable forom evrywhere, where it is included without doint something like `extern const struc_t DEFAULT_STRUC`? than that would be fine for me, i just want a struct that contains thoose values without to declare + initialize it explicit. – dhein Oct 16 '13 at 19:19
  • And I like to use size_t just because it is as far as I know the size of cpu orating width. So... I jsut like size_t I don't have a realy good reason for. – dhein Oct 16 '13 at 19:21
  • Updated answer with an `#ifndef` that'll make sure this does what you want. That said, there's nothing wrong with putting the `DEFAULT_STRUC` in a .c file and doing `extern const struc_t DEFAULT_STRUC` in the header. – Christian Ternus Oct 16 '13 at 19:22
  • ah i guess you missunderstood me, but anyway, your answer does exactly what I'm looking for, thanks. – dhein Oct 16 '13 at 19:35
  • Glad to be of service, then :) – Christian Ternus Oct 16 '13 at 19:35
  • It can also be done with a define: `#define DEFAULT_STRUC ((const struc_t) {.cost = 2000, .dmg = 100})` – Nelson Jun 05 '22 at 16:49
1
typedef struct struc_s
{
int a;
int b;    
}s; 

This is type definition and Not a declaration of object.You can initialize at the time of declaring object.

C89-style initializers are used when contiguous members may be given.

s obj={1,2}; 

For non contiguous or out of order members list, designated initializer style may be used

s obj={.a=1,.b=2};  

     or

s obj={.b=2,.a=1};

A third way is copy the value of an existing object of the same type

s obj1=obj;
Gangadhar
  • 10,248
  • 3
  • 31
  • 50