const in typedef struct?

S

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.


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
 
A

Alexander Bartolich

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.
 
M

Mark A. Odell

(e-mail address removed) (S Austin) wrote in

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
 
D

donLouis

On 2 Jan 2004 08:31:24 -0800
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top