Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem about const member in a struct

Reply
Thread Tools

problem about const member in a struct

 
 
Gestorm
Guest
Posts: n/a
 
      06-14-2008
Hi everyone, I have a problem. If I declare a struct with a const
member, what will happen?For example:
if I declared a struct like following:
struct{
const int a;
char c;
}aStruct;
then such statement as
aStruct.a = 0;
is illegal.
But I can printf the value of a, it's always the same value no matter
how many times I recompile the program. I'm wondering about how can
the compiler ascertain the value of the const member?
In an application, I wanna define a struct, whose first member is a
const and its value is given by me, how can I do that? Neither K&R
book nor "C: A Reference manual" mention this problem. Does anyone
know? Thanx ^_^
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-14-2008
Gestorm wrote:
> Hi everyone, I have a problem. If I declare a struct with a const
> member, what will happen?For example:
> if I declared a struct like following:
> struct{
> const int a;
> char c;
> }aStruct;
> then such statement as
> aStruct.a = 0;
> is illegal.
> But I can printf the value of a, it's always the same value no matter
> how many times I recompile the program. I'm wondering about how can
> the compiler ascertain the value of the const member?


It can be initialised:

struct{
const int a;
char c;
} aStruct = { 42,'a' };

> In an application, I wanna define a struct, whose first member is a
> const and its value is given by me, how can I do that? Neither K&R
> book nor "C: A Reference manual" mention this problem. Does anyone
> know? Thanx ^_^


Make the struct a type:

struct X {
const int a;
char c;
};

Then you can use one:

void f( int n )
{
struct X x = { n };
x.c = 'n';
}

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-14-2008
Gestorm <(E-Mail Removed)> writes:
> Hi everyone, I have a problem. If I declare a struct with a const
> member, what will happen?For example:
> if I declared a struct like following:
> struct{
> const int a;
> char c;
> }aStruct;
> then such statement as
> aStruct.a = 0;
> is illegal.
> But I can printf the value of a, it's always the same value no matter
> how many times I recompile the program. I'm wondering about how can
> the compiler ascertain the value of the const member?
> In an application, I wanna define a struct, whose first member is a
> const and its value is given by me, how can I do that? Neither K&R
> book nor "C: A Reference manual" mention this problem. Does anyone
> know? Thanx ^_^


An object declared as "const" has the value given to it when it's
initialized. That value cannot legally be changed later by an
assignment.

You didn't tell us where you declared "aStruct". It's (almost) always
best to post a complete compilable program that demonstrates your
point.

If aStruct is declared outside any function, or with the "static"
keyword, then the initial value of aStruct.a will be 0. If it's
declared inside a function with no "static" keyword, its initial value
will be garbage, and you won't be able to assign a valid value. (It's
not unlikely that the garbage will happen to be 0, but don't depend on
it.)

Applying "const" to members of struct actually isn't very common in my
experience. But here's a small program that might suggest how it
could be useful:

#include <stdio.h>

struct person {
const int birth_year;
int current_age;
};

int main(void)
{
struct person fred = { 1970, 38 };
printf("fred.birth_year = %d, fred.current_age = %d\n",
fred.birth_year, fred.current_age);
/* Can't change fred.birth_year */
fred.current_age ++;
printf("fred.birth_year = %d, fred.current_age = %d\n",
fred.birth_year, fred.current_age);
return 0;
}

birth_year must be set when the person object is created, and cannot
be changed thereafter. current_age can change over time.

(A flaw in this is that C lets you get away with *not* initializing
birth_year; if you don't initialize it, you can't set it later.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Gestorm
Guest
Posts: n/a
 
      06-14-2008

> It can be initialised:
>
> struct{
> const int a;
> char c;
>
> } aStruct = { 42,'a' };


> Make the struct a type:
>
> struct X {
> const int a;
> char c;
>
> };
>


> Ian Collins.

I understand, thank you very much!
 
Reply With Quote
 
Gestorm
Guest
Posts: n/a
 
      06-14-2008

> You didn't tell us where you declared "aStruct". It's (almost) always
> best to post a complete compilable program that demonstrates your
> point.
>

Sorry! I would take notice next time!

>
> Applying "const" to members of struct actually isn't very common in my
> experience. But here's a small program that might suggest how it
> could be useful:
>
> #include <stdio.h>
>
> struct person {
> const int birth_year;
> int current_age;
>
> };
>
> int main(void)
> {
> struct person fred = { 1970, 38 };
> printf("fred.birth_year = %d, fred.current_age = %d\n",
> fred.birth_year, fred.current_age);
> /* Can't change fred.birth_year */
> fred.current_age ++;
> printf("fred.birth_year = %d, fred.current_age = %d\n",
> fred.birth_year, fred.current_age);
> return 0;
>
> }
>
> birth_year must be set when the person object is created, and cannot
> be changed thereafter. current_age can change over time.
>
> (A flaw in this is that C lets you get away with *not* initializing
> birth_year; if you don't initialize it, you can't set it later.)
>

Very clearly thanx a lot!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-14-2008
Gestorm <(E-Mail Removed)> writes:
>> You didn't tell us where you declared "aStruct". It's (almost) always
>> best to post a complete compilable program that demonstrates your
>> point.
>>

> Sorry! I would take notice next time!


I wrote the above, starting with "You didn't tell us ...". Your
newsreader, or in this case the Google Groups interface, automatically
adds an attribution line, such as "Gestorm <(E-Mail Removed)>
writes:" above. Please don't delete it. It helps keep track of who
said what, and it's just polite to give credit when quoting someone
else's words.

It's also rarely necessary to quote an entire article when posting a
followup. Trim quoted material to just what's relevant.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Gestorm
Guest
Posts: n/a
 
      06-15-2008
On 6月14日, 下午11时23分, Keith Thompson <(E-Mail Removed)> wrote:

> I wrote the above, starting with "You didn't tell us ...". Your
> newsreader, or in this case the Google Groups interface, automatically
> adds an attribution line, such as "Gestorm <(E-Mail Removed)>
> writes:" above. Please don't delete it. It helps keep track of who
> said what, and it's just polite to give credit when quoting someone
> else's words.
>
> It's also rarely necessary to quote an entire article when posting a
> followup. Trim quoted material to just what's relevant.
>

OK! I'm a newer. Thanks for telling me those rules!^_^
 
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
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments