linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* constant variable
@ 2004-03-11 11:19 Nasir Simbolon
  2004-03-11 13:42 ` Steven Smith
  0 siblings, 1 reply; 3+ messages in thread
From: Nasir Simbolon @ 2004-03-11 11:19 UTC (permalink / raw)
  To: linux-c-programming

Hi All, 
I just curious. I have the following codes

main()
{
 	const int i=9;          //declare constant  integer variable and initialize
       	int* j=NULL;   		     //declare a pointer to integer
	 j = (int*) &i;         //j point to address of memory i 
 	*j = 3;		     //modify value  of  j point to 

	//Now print the result
	 printf("value of i = %d\n",i);
  	 printf("value of j = %d\n",*j);
	 printf("Address of i = %x\n",&i);
 	 printf("Address of j = %x\n",j);
}
when I compile using gcc version 3.3.2 (Debian) and run, I have the result :

value of i = 9
value of *j = 3
Address of i = bffff8a4
Address of j = bffff8a4

Now, I notice  that j point to address of i BUT they do not  have the same 
value.

Anybody have an explanation?


Regards,

Nasir 


-- 
"timor Domini principium scientia (Prov 1:7)"


^ permalink raw reply	[flat|nested] 3+ messages in thread
* RE: constant variable
@ 2004-03-11 13:28 Sandro Dangui
  0 siblings, 0 replies; 3+ messages in thread
From: Sandro Dangui @ 2004-03-11 13:28 UTC (permalink / raw)
  To: nasir, linux-c-programming


If you look at the generated assembly you will notice that the compiler uses
(in the call printf("value of i = %d\n",i);) the value 9 instead of the
contents of i. I mean, the assembly pushes directly the value 9 to the
stack... It assumes that if you declared a constant it will not be changed.

[]'s

Sandro.


-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of Nasir
Simbolon
Sent: Thursday, March 11, 2004 8:19 AM
To: linux-c-programming@vger.kernel.org
Subject: constant variable


Hi All, 
I just curious. I have the following codes

main()
{
 	const int i=9;          //declare constant  integer variable and
initialize
       	int* j=NULL;   		     //declare a pointer to integer
	 j = (int*) &i;         //j point to address of memory i 
 	*j = 3;		     //modify value  of  j point to 

	//Now print the result
	 printf("value of i = %d\n",i);
  	 printf("value of j = %d\n",*j);
	 printf("Address of i = %x\n",&i);
 	 printf("Address of j = %x\n",j);
}
when I compile using gcc version 3.3.2 (Debian) and run, I have the result :

value of i = 9
value of *j = 3
Address of i = bffff8a4
Address of j = bffff8a4

Now, I notice  that j point to address of i BUT they do not  have the same 
value.

Anybody have an explanation?


Regards,

Nasir 


-- 
"timor Domini principium scientia (Prov 1:7)"

-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

end of thread, other threads:[~2004-03-11 13:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-11 11:19 constant variable Nasir Simbolon
2004-03-11 13:42 ` Steven Smith
  -- strict thread matches above, loose matches on Subject: below --
2004-03-11 13:28 Sandro Dangui

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