473,405 Members | 2,262 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,405 software developers and data experts.

Is there any good way to do it

Hello I am writing a big program.

I have define a struction

typedef struct
{
int Name;
int Year;
}birth;

Now I'd like to define a sequence of A, such as

Birth A = {1,1990}
Birth B = {1,1991}
.....
I hope to use #define so that A,B can be used as constant.
Can I?
and is it a good way of programming?

Thanks

Nov 15 '05 #1
3 1064
In article <11*********************@z14g2000cwz.googlegroups. com>,
<qi*****@gmail.com> wrote:
I have define a struction typedef struct
{
int Name;
int Year;
}birth; Now I'd like to define a sequence of A, such as Birth A = {1,1990}
Birth B = {1,1991}


birth A = {1,1990};
birth B = {1,1991};

:I hope to use #define so that A,B can be used as constant.
:Can I?

No.

What you can do is

#define B90 {1,1990}
#define B91 {1,1991}

birth A = B90;
birth B = B91;

and use B90 or B91 wherever else in the code that you happen to need
to initialize new variables to those values.

But A and B will be writable. You cannot create an unwritable structure
with any particular contents: the closest you can get is, as in the
above, to define textual substituations that happen to expand to the
values you need.

You could also get fancier with functions that return const pointers
to structures. You could probably even have something like

const birth B90(void) { birth B90_temp = {1,1990}; return B90_temp; }

but returning whole structures tends to make old-time programmers
queasy.

--
Watch for our new, improved .signatures -- Wittier! Profounder! and
with less than 2 grams of Trite!
Nov 15 '05 #2
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11*********************@z14g2000cwz.googlegroups. com>,
<qi*****@gmail.com> wrote:
I have define a struction
typedef struct
{
int Name;
int Year;
}birth;

Now I'd like to define a sequence of A, such as

Birth A = {1,1990}
Birth B = {1,1991}


birth A = {1,1990};
birth B = {1,1991};

:I hope to use #define so that A,B can be used as constant.
:Can I?

No.

What you can do is

#define B90 {1,1990}
#define B91 {1,1991}

birth A = B90;
birth B = B91;

and use B90 or B91 wherever else in the code that you happen to need
to initialize new variables to those values.


Right, the {1,1990} construct can be used in an initializer, but it
can't be used as an expression. C99 has compound literals, but
they're not universally supported yet.
But A and B will be writable. You cannot create an unwritable structure
with any particular contents: the closest you can get is, as in the
above, to define textual substituations that happen to expand to the
values you need.


Of course you can:

const birth A = { 1, 1990 };
const birth B = { 2, 1991 };

A and B aren't true "constants"; they're best thought of as read-only
variables. But they're probably suitable for the OP's purposes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3

Walter Roberson wrote:
birth A = {1,1990};
birth B = {1,1991};

:I hope to use #define so that A,B can be used as constant.
:Can I?

No.
The answer is correct for C89
birth A = B90;
birth B = B91;

and use B90 or B91 wherever else in the code that you happen to need
to initialize new variables to those values.

But A and B will be writable. You cannot create an unwritable structure
with any particular contents: the closest you can get is, as in the
Actually, you can define const objects. Except for odd uses of strchr
etc. or by use of casts, attempts to write to them always involves a
constraint violation; in the exceptional cases, it invokes undefined
behaviour. So, they are kind of unwritable.

You could also get fancier with functions that return const pointers
to structures. You could probably even have something like

const birth B90(void) { birth B90_temp = {1,1990}; return B90_temp; }
A definition of a function type which is incompatible with birth(void),
but which is functionally identical.

but returning whole structures tends to make old-time programmers
queasy.


But using qualified values in C rather than qualified objects just to
make someone queasy!

Nov 15 '05 #4

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

Similar topics

10
by: KN | last post by:
I know both are pretty much the same and it comes down to personal choice. But I have to make the choice for the team. Things so far that I am considering 1. XML documentation in C# -- thats...
29
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
59
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
17
by: Brett | last post by:
I'd like references on where to find some good (quality) icons to use for form and application icons (for the EXE). Most of the free stuff isn't that great looking and there isn't a good...
15
by: Alex L Pavluck | last post by:
I am new to programming other than SAS. I read that C# is a good starting language and I have started to create some simple programs with C# 2005 express edition. Can someone let me know if this...
6
by: Jamiil | last post by:
I am not a programmer by any means, but a dedicated aficionado. I have good understanding of Java and C/C++, and now I would like to learn javascript->ajax, but I don't know where to start. My HTML...
30
by: mistral | last post by:
Neeed good javascript unescape encoder, to protect javascript code. Some advices? Online tool, or ready javascript only. Any opinions about the Javascript Obfuscator:...
244
by: Ajinkya | last post by:
Can anyone suggest me a good compiler for(c/cpp) for windows? I tried dev cpp but its debugging facility is very poor.
76
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...

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.