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

problem about const member in a struct

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 ^_^
Jun 27 '08 #1
6 1778
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.
Jun 27 '08 #2
Gestorm <zh********@126.comwrites:
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) ks***@mib.org <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"
Jun 27 '08 #3
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!
Jun 27 '08 #4
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!
Jun 27 '08 #5
Gestorm <zh********@126.comwrites:
>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 <zh********@126.com>
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) ks***@mib.org <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"
Jun 27 '08 #6
On 6ÔÂ14ÈÕ, ÏÂÎç11ʱ23·Ö, Keith Thompson <ks...@mib.orgwrote:
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 <zhcfree...@126.com>
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!^_^
Jun 27 '08 #7

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
4
by: Martin MacRobert | last post by:
Hi Gang, The following code does not compile, but I can't figure out why. The compiler complains that the CuriouslyDerivedType (CRDerived) does not publish the "value_type", yet in fact the class...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
16
by: aburry | last post by:
/* test.c */ #include <stdlib.h> void f() { struct X { const int x; }; struct X* myx = malloc(sizeof(struct X)); myx->x = 42; } /* end of test.c */
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: 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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.