problem about const member in a struct

Discussion in 'C Programming' started by Gestorm, Jun 14, 2008.

  1. Gestorm

    Gestorm Guest

    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 ^_^
     
    Gestorm, Jun 14, 2008
    #1
    1. Advertising

  2. Gestorm

    Ian Collins Guest

    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.
     
    Ian Collins, Jun 14, 2008
    #2
    1. Advertising

  3. Gestorm <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 14, 2008
    #3
  4. Gestorm

    Gestorm Guest


    > 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!
     
    Gestorm, Jun 14, 2008
    #4
  5. Gestorm

    Gestorm Guest


    > 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!
     
    Gestorm, Jun 14, 2008
    #5
  6. Gestorm <> 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 <>
    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) <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"
     
    Keith Thompson, Jun 14, 2008
    #6
  7. Gestorm

    Gestorm Guest

    On 6ÔÂ14ÈÕ, ÏÂÎç11ʱ23·Ö, Keith Thompson <> 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 <>
    > 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!^_^
     
    Gestorm, Jun 15, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,438
    Chris Fogelklou
    Apr 20, 2004
  2. Replies:
    11
    Views:
    1,151
  3. Javier
    Replies:
    2
    Views:
    621
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,171
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    945
    fungus
    Oct 31, 2008
Loading...

Share This Page