linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* problem with const in structure
@ 2003-01-06 13:23 Mohammed Khalid Ansari
  2003-01-06 15:48 ` Glynn Clements
  0 siblings, 1 reply; 3+ messages in thread
From: Mohammed Khalid Ansari @ 2003-01-06 13:23 UTC (permalink / raw)
  To: linux c programming mailing list


Hi,

consider the following code.

####
#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?

with regards...

-- 

**************************************************************************

Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
Assistant Manager II                          (off) : 0091-022-2024641
National Centre for Software Technology   Fax       : 0091-022-2049573 
8th flr,Air India Build. Nariman Point,   E-Mail    : khalid@ncst.ernet.in 	
Mumbai 400021.

Homepage : http://soochak.ncst.ernet.in/~khalid			  	  

**************************************************************************



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: problem with const in structure
  2003-01-06 13:23 Mohammed Khalid Ansari
@ 2003-01-06 15:48 ` Glynn Clements
  0 siblings, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2003-01-06 15:48 UTC (permalink / raw)
  To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list


Mohammed Khalid Ansari wrote:

> consider the following code.
> 
> ####
> #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?

In C, the "const" keyword only has a defined meaning when applied to
arrays or pointers. Even then, it isn't required that the compiler
prohibits modification.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: problem with const in structure
@ 2003-01-06 17:46 
  0 siblings, 0 replies; 3+ messages in thread
From:  @ 2003-01-06 17:46 UTC (permalink / raw)
  To: linux-c-programming

> #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.





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-01-06 17:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-01-06 17:46 problem with const in structure 
  -- strict thread matches above, loose matches on Subject: below --
2003-01-06 13:23 Mohammed Khalid Ansari
2003-01-06 15:48 ` Glynn Clements

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).