473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

const in typedef struct?

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
3 34691
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: j6vflbl6vy6g8o001 | last post by:
Sorry if this is really dumb, but I've searched for an example like this and although can find many structures containing references back to themselves, none with the additional complexities I see...
2
by: Steven T. Hatton | last post by:
typedef struct { unsigned char e_ident; Elf32_Half e_type; Elf32_Half e_machine; Elf32_Word e_version; Elf32_Addr e_entry; Elf32_Off e_phoff; Elf32_Off e_shoff; Elf32_Word e_flags;...
2
by: Neil McPhail | last post by:
I'm new to C (and Usenet) and have been using the dreaded Schildt's Complete C Reference. I appreciate this may not have been the best idea, so no need to point this out! On page 546 there is a...
8
by: J Krugman | last post by:
My compiler complains if I do something like this typedef struct { node *next; } node; but it's OK with typedef struct superfluous_identifier { superfluous_identifier *next;
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
5
by: Russell Shaw | last post by:
Hi, In setjmp.h on a linux system, there is: typedef struct __jmp_buf_tag { ... } jmp_buf; I could understand typedef struct { } jmp_buf, but how do i interpret typedef struct { } jmp_buf ?
23
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.