473,462 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Initialization of an array and symbolic names for index

Hi all!

In source.h, I define a struct and the symbolic names of the indexes:
--- source.h ---
typedef struct {
type field1;
type field2;
...
type fieldm;
} mystruct;

extern mystruct mys;

enum MYSTRUCT_INDEX {
RECORD1 = 0,
RECORD2,
...
RECORDn,
};
---

In source.c, I initialize the struct:
--- source.c ---
#include "source.h"

mystruct mys[] = {
{ <field11>, <field12>, ..., <field1m},
{ <field21>, <field22>, ..., <field2m},
...
{ <fieldn1>, <fieldn2>, ..., <fieldnm},
};
---
In this way, in a third C source file I can include "source.h" and access
the array by the symbolic names:
--- third.c ---
#include "source.h"

....
if( mys[RECORDx].fieldy==... )
....
---

This system works well but there is a drawback. When I insert a new record in
the middle of the array I should remember to change and re-align the enumeration
in the source.h file. Is there a way, maybe using some preprocessor directives,
so that I modify/reorder/insert the records only in a point and maintain the
coherence in the enumeration?
Apr 28 '07 #1
3 1986
On Sat, 28 Apr 2007 09:18:32 GMT, pozz <pN************@gmail.com>
wrote:
>Hi all!

In source.h, I define a struct and the symbolic names of the indexes:
--- source.h ---
typedef struct {
type field1;
type field2;
...
type fieldm;
} mystruct;

extern mystruct mys;

enum MYSTRUCT_INDEX {
RECORD1 = 0,
RECORD2,
...
RECORDn,
};
---

In source.c, I initialize the struct:
--- source.c ---
#include "source.h"

mystruct mys[] = {
{ <field11>, <field12>, ..., <field1m},
{ <field21>, <field22>, ..., <field2m},
...
{ <fieldn1>, <fieldn2>, ..., <fieldnm},
};
---
In this way, in a third C source file I can include "source.h" and access
the array by the symbolic names:
--- third.c ---
#include "source.h"

...
if( mys[RECORDx].fieldy==... )
...
---

This system works well but there is a drawback. When I insert a new record in
How can this work? Your header file declares mys to be a single
struct. Your code in third.c accesses mys as an array. You should be
getting a syntax error.
>the middle of the array I should remember to change and re-align the enumeration
in the source.h file. Is there a way, maybe using some preprocessor directives,
so that I modify/reorder/insert the records only in a point and maintain the
coherence in the enumeration?
After you fix the above problem, the answer is still no.

Adding a new element to the array is done by updating source.c and
then compiling it. At this point in time, source.h must exist in
order to be included. Anything you do at this time, such as
preprocessing directives in source.c, will affect only the object file
being generated. Nothing will ever be written back to source.h.
#include is an input operation, not an update one. When you compile
third.c, source.h does not "remember" anything that happened while
compiling source.c.
Remove del for email
Apr 29 '07 #2
Barry Schwarz ha scritto:
>This system works well but there is a drawback. When I insert a new record in
How can this work? Your header file declares mys to be a single
struct. Your code in third.c accesses mys as an array. You should be
getting a syntax error.
You are right. In source.h I should declare:
--- source.h ---
....
extern mystruct mys[];
....
---

>the middle of the array I should remember to change and re-align the enumeration
in the source.h file. Is there a way, maybe using some preprocessor directives,
so that I modify/reorder/insert the records only in a point and maintain the
coherence in the enumeration?

After you fix the above problem, the answer is still no.

Adding a new element to the array is done by updating source.c and
then compiling it. At this point in time, source.h must exist in
order to be included. Anything you do at this time, such as
preprocessing directives in source.c, will affect only the object file
being generated. Nothing will ever be written back to source.h.
#include is an input operation, not an update one. When you compile
third.c, source.h does not "remember" anything that happened while
compiling source.c.
Ok, I agree with you, but I needn't to have the same structure of files as
above. I can use a third file (array.def) to list the names and the values and
include it in source.h and source.c, of course with some preprocessing tricks.

I was thinking about the following solution.

--- array.def ---
MACRO( RECORD1, value1 )
MACRO( RECORD2, value2 )
....
MACRO( RECORDn, valuen )
---

--- source.h ---
#undef MACRO
#define MACRO(sym,val) sym,

typedef struct {
int value;
} mystruct;

extern mystruct mys[];

enum MYSTRUCT_INDEX = {
#include "array.def"
};
---

--- source.c ---
#undef MACRO
#define MACRO(sym,val) { val },

mystruct mys[] = {
#include "array.def"
};
---
The above solution works well, but there is a little difference with the first
scenario. Here the struct mystruct is composed by only one member. My real
struct is composed by several fields.
In this case, I should define MACRO in source.c and source.h in the following way:
--- source.h ---
....
#undef MACRO
#define MACRO(sym, f1, f2, ..., fm) sym,
....
---
--- source.c ---
....
#undef MACRO
#define MACRO(sym, f1, f2, ..., fm) { f1, f2, ..., fm },
....
---

If I change the composition of the struct, I should modify the MACRO definition
in source.c and source.h, so I must continue preserving the coeherence of two or
three files.
Apr 29 '07 #3
On Sun, 29 Apr 2007 15:08:23 GMT, pozz <pN************@gmail.com>
wrote:
>Barry Schwarz ha scritto:
snip issues no longer in question
>I was thinking about the following solution.

--- array.def ---
MACRO( RECORD1, value1 )
MACRO( RECORD2, value2 )
...
MACRO( RECORDn, valuen )
---

--- source.h ---
#undef MACRO
#define MACRO(sym,val) sym,

typedef struct {
int value;
} mystruct;

extern mystruct mys[];

enum MYSTRUCT_INDEX = {
#include "array.def"
};
---

--- source.c ---
#undef MACRO
#define MACRO(sym,val) { val },

mystruct mys[] = {
#include "array.def"
};
---
The above solution works well, but there is a little difference with the first
scenario. Here the struct mystruct is composed by only one member. My real
struct is composed by several fields.
In this case, I should define MACRO in source.c and source.h in the following way:
--- source.h ---
...
#undef MACRO
#define MACRO(sym, f1, f2, ..., fm) sym,
...
---
--- source.c ---
...
#undef MACRO
#define MACRO(sym, f1, f2, ..., fm) { f1, f2, ..., fm },
...
---

If I change the composition of the struct, I should modify the MACRO definition
in source.c and source.h, so I must continue preserving the coeherence of two or
three files.
To consolidate the point of change to a single (header) file which has
the additional advantage of insuring that all source files that
include the header will be recompiled when the header changes (if your
system keeps track), I would try something along the lines of

#define MACRO1(sym, f1, f2, ..., fm) sym
#define MACRO2(sym, f1, f2, ..., fm) {f1, f2, ..., fm}
#define MACRO(macro) \
macro(sym1, <field11>, <field21>, ..., <fieldm1>), \
macro(sym2, <field12>, <field22>, ..., <fieldm2>), \
...,
macro(symn, <field1n>, <field2n, ..., <fieldmn>)

enum MYSTRUCT_INDEX {
mystruct_dummy = -1, /* probably not necessary since enums
default to start at 0 but your initial code had initialization */
MACRO(MACRO1)
};

and in source.c
mystruct mys[] = {MACRO(MACRO2)};

I'm not sure if the string of macros above requires any kind of
"circumlocution" along the lines described in faq section 11.17.


Remove del for email
Apr 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Sandeep Sharma | last post by:
Right from the time the first edition of K&R was released, the advantages of using symbolic constants, as opposed to "magic numbers", has been emphasized ---- and for good reason. I don't dispute...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
3
by: Eric Lilja | last post by:
Assignment: Create a 3x4 2-dimensional array of integers. Populate each element using some non-random scheme so that no two elemens contain the same value. Then create two functions for printing...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
4
by: tasahmed | last post by:
Hello Friends, I wrote a function which scans the current working directory and lists out details such as directory/file owner, permission etc. The output of this script can be viewing in the...
1
by: amygrant1701 | last post by:
Hi, I've done this before so I don't see what I could doing wrong here. I'm running mysql 5x on freebsd. I'm using the default data directory of "/var/db/mysql" In there I have several dozen...
8
by: Yarco | last post by:
Array is the very basic type in php i think. So why const keyword doesn't support such defination grammar? class Date { const char3 = array(1 ='Jan', ...); // ?? } So how can i do somthing...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.