The const keyword and structures

Discussion in 'C Programming' started by kelvSYC, Oct 10, 2003.

  1. kelvSYC

    kelvSYC Guest

    If you have an instance of a struct that's marked const, does the const
    keyword mean that all of its members are read-only? What if you wanted
    a struct where some members are constants and a few others you could
    change?

    Example:

    struct foo {
    const int a;
    int b;
    int (*c)(void);
    }

    const struct foo bar;

    bar.a is constant
    Is bar.b constant?
    How about bar.c?

    What if you wanted to have a structure where a and c are constant, and
    b could be changed? Would you have to remove the const when declaring
    bar?

    --
    I am only a mirage.
    kelvSYC, Oct 10, 2003
    #1
    1. Advertising

  2. kelvSYC

    Mike Wahler Guest

    "kelvSYC" <> wrote in message
    news:091020032155120666%...
    > If you have an instance of a struct that's marked const, does the const
    > keyword mean that all of its members are read-only?


    It means the struct object cannot be modified.

    > What if you wanted
    > a struct where some members are constants and a few others you could
    > change?
    >
    > Example:
    >
    > struct foo {
    > const int a;
    > int b;
    > int (*c)(void);
    > }
    >
    > const struct foo bar;
    >
    > bar.a is constant
    > Is bar.b constant?


    Yes, by implication (the whole object 'bar' is const).

    > How about bar.c?


    Same thing.

    >
    > What if you wanted to have a structure where a and c are constant, and
    > b could be changed? Would you have to remove the const when declaring
    > bar?


    Yes.

    -Mike
    Mike Wahler, Oct 10, 2003
    #2
    1. Advertising

  3. kelvSYC

    Noah Roberts Guest

    kelvSYC wrote:
    > If you have an instance of a struct that's marked const, does the const
    > keyword mean that all of its members are read-only? What if you wanted
    > a struct where some members are constants and a few others you could
    > change?
    >
    > Example:
    >
    > struct foo {
    > const int a;
    > int b;
    > int (*c)(void);
    > }
    >
    > const struct foo bar;
    >
    > bar.a is constant
    > Is bar.b constant?
    > How about bar.c?


    Yes, but if you declaired it like, "struct foo bar;" then it wouldn't be.

    NR
    Noah Roberts, Oct 10, 2003
    #3
    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. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  2. coala
    Replies:
    3
    Views:
    372
    coala
    Sep 6, 2006
  3. coala
    Replies:
    1
    Views:
    588
    Victor Bazarov
    Sep 6, 2006
  4. Replies:
    11
    Views:
    1,100
  5. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
Loading...

Share This Page