By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

const in typedef struct?

P: n/a
I would like to define a structure (a file header, in this case) that
includes certain constants, and ensure that those members of the
structure are always initialized to the same value.
Something like

#define MY_MAGIC_NUMBER 0x12345678

typedef struct {
const unsigned long magic_number = MY_MAGIC_NUMBER ;

/* more stuff ... */

} FILE_HEADER ;

but obviously I can't put an initializer inside a typedef declaration.

What's a good way to do this? I can define a macro that does the
initialization (if I leave off the "const" qualifier on magic_number),
but then I have to depend on the programmer remembering to call the
macro with each instance of a FILE_HEADER struct. I'd rather have the
initialization done automatically.

Thanks
S. Austin
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
begin followup to S Austin:
I would like to define a structure (a file header, in this case)
that includes certain constants, and ensure that those members of
the structure are always initialized to the same value.
C++ can do this through its concept of 'constructor'. The complete
story is rather complicated though, and so far C is keeping true to
the spirit of a simple language.
[...] What's a good way to do this?


Either rely on a competent programmer.
Or go all the way to an opaque data structure.
The most prominent example is struct FILE.
Nothing is known about its data members.
There is fopen to create it, and fclose to destroy.

--
Für Google, Tux und GPL!
Nov 14 '05 #2

P: n/a
Fu***********@mindspring.com (S Austin) wrote in
news:a4**************************@posting.google.c om:
I would like to define a structure (a file header, in this case) that
includes certain constants, and ensure that those members of the
structure are always initialized to the same value.
Something like

#define MY_MAGIC_NUMBER 0x12345678

typedef struct {
const unsigned long magic_number = MY_MAGIC_NUMBER ;

/* more stuff ... */

} FILE_HEADER ;

but obviously I can't put an initializer inside a typedef declaration.


You just can't give it the initial value until you define the object. Try
this:

#include <stdlib.h>

typedef struct FooBar
{
const int apple; /* const! */
volatile char *ptr;
} FooBar;

int main(void)
{
FooBar foo = { 1, NULL }; /* typedef version */
struct FooBar bar = { 1, NULL }; /* struct version */

return foo.apple == bar.apple ? EXIT_SUCCESS : EXIT_FAILURE;
}

and then compiler with:

gcc -g -O -c -Wall -ansi -pedantic foo.c -o foo.o

--
- Mark ->
--
Nov 14 '05 #3

P: n/a
On 2 Jan 2004 08:31:24 -0800
Fu***********@mindspring.com (S Austin) wrote:
I would like to define a structure (a file header, in this case) that
includes certain constants, and ensure that those members of the
structure are always initialized to the same value.
Something like

#define MY_MAGIC_NUMBER 0x12345678

typedef struct {
const unsigned long magic_number = MY_MAGIC_NUMBER ;

/* more stuff ... */

} FILE_HEADER ;

but obviously I can't put an initializer inside a typedef declaration.

What's a good way to do this? I can define a macro that does the
initialization (if I leave off the "const" qualifier on magic_number),
but then I have to depend on the programmer remembering to call the
macro with each instance of a FILE_HEADER struct. I'd rather have the
initialization done automatically.


In order to "automatically" initialize variables, you need a C source
file. In header.h put,

#define MY_MAGIC_NUMBER 0x12345678
/* other magic number defines */

typedef struct FILE_HEADER {
const unsigned long magic_number;
/* whatever else you need */
} FILE_HEADER;

extern struct FILE_HEADER foo;

then, in say, init.c put,

#include "header.h"

struct FILE_HEADER foo = { MY_MAGIC_NUMBER /*, more inits */ };

then, when the user includes header.h, they can use foo,

#include <stdio.h>
#include "header.h"

int main(void) {
printf("%lu\n", foo.magic_number);
return 0; }

for whatever they need. init.c would need to be compiled as an
object file, then linked in when the final program is made.

--
donLouis
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.