Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is there any good way to do it

Reply
Thread Tools

Is there any good way to do it

 
 
qianz99@gmail.com
Guest
Posts: n/a
 
      10-12-2005
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

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      10-12-2005
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
> In article <(E-Mail Removed). com>,
> <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
tanmoy87544@gmail.com
Guest
Posts: n/a
 
      10-13-2005

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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Are there ANY good forums out there anymore??? Eric Danstron ASP General 1 09-22-2005 01:37 PM
is there a way ..... any way Andries Perl Misc 27 04-27-2004 06:15 AM
Is there good software out there to id and remove trojan progams?? WyleCoyote@cactus.com Computer Security 2 10-10-2003 07:43 PM
Is there any way to make this look good. mike HTML 3 08-09-2003 03:29 PM



Advertisments