linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
To: linux-c-programming@vger.kernel.org
Subject: Re: problem with const in structure
Date: Mon, 6 Jan 2003 18:46:13 +0100	[thread overview]
Message-ID: <000201c2b5d4$544aaef0$0200a8c0@earth> (raw)

> #include <stdio.h>
>
> struct node {
> const int x;
> char name[20];
> struct node *next;
> };
>
> int main()
> {
> struct node newnode;
>
> newnode.x = 10;
> newnode.x = 12;
> printf ("x = %d\n", newnode.x);
> return 0;
> }
> ####
>
> Now, you can see that even though I have defined x to be const int, I can
> assign it values. When I compiled the program, it gives the warning that
> assignment of read-only member x but when I ran the program, it worked
> fine. Where is the catch. How do I define a structure variable to be
> const?

Welcome in C World    (which is mainly non const :))
In C++  it's easy to fix
struct node
{
    node(int x_, node *  left_=0, node *  right_=0) : x(x_), left(left_),
right(right_)
    {
        x = x_;    // is *always* compile time error (in C++), because
                       // you are altering const variable
         // in C++ you have to be aware that assignment and initialization
are *not* the same thing
         // they might seem for some to act similar and in some contexts
they do
    }
    // const node * root;
    const int x;
    node * left, * right;
};

int i = 0; // is initialisation of non const varibale
i = 0;      // assignment
const int null;    // error, const must be initialized
null = 0;    // error
const int null = 0;    // ok, in this way or
const int null(0);      // or
const int null = int(0);


Daniel.

--
$>fortune
the generation of random numbers is too important
to be left to chance.





             reply	other threads:[~2003-01-06 17:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-06 17:46  [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-01-06 13:23 problem with const in structure Mohammed Khalid Ansari
2003-01-06 15:48 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='000201c2b5d4$544aaef0$0200a8c0@earth' \
    --to=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).