[Dev-C++] Another pointer question (I think its an compiler issue)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Christian S. <sm...@xs...> - 2003-05-27 19:14:19
|
Thanks for your answer Avik.
I'm now working another question from the book. However I seem to have a
different result than theory would dictate.
Look at and perhaps run the commented source code:
#include <stdio.h>
main ( )
{
double flt_num, *flt_ptr;
flt_num = 123,45;
/* This bit shows (prints) the value of flt_num and then
it's adress in memory */
printf ("flt_num = %f.\n",flt_num);
printf ("The adress of flt_num is %p.\n",&flt_num);
/* Assigning the pointer to the variable flt_num */
flt_ptr = &flt_num;
/* Setting to value of flt_num via a pointer variable to the
new value */
*flt_ptr = 543,21;
/* Here comes my problem. If I would ask for the value of the pointer.
Its called flt_ptr btw. In theory it should show the memory address of
the variable flt_num. But in MSV++ 6 it shows 0.000000 and in DevCPP the
value of *flt_ptr. Is this a feature or a bug? */
/* Now I'm priting the value of the pointer to screen */
printf ("flt_ptr = %f.\n",flt_ptr); /* This should be == to &flt_num but
it isn't!!*/
printf ("The adress of flt_ptr is %p.\n",&flt_ptr);
printf ("*flt_ptr = %f.\n", *flt_ptr);
return 0;
}
-----Original Message-----
From: Avik Ghose [mailto:a_g...@ya...]
Sent: dinsdag 27 mei 2003 20:52
To: Christian Schmidt
Subject: Re: [Dev-C++] Question about pointers (beginner)
Hi Christian,
the very statement
*ptr_int = 456;
is malicious. Cause the pointer right now is neither null(except if your
compiler is kind enough to do so) nor it points to any valid address
assigned to your programs run-time memory or the heap.
As to what your book says, that is completely impossible to happen. If the
program at all runs without any run-time error, the value of *ptr_int will
be 123.
Christian Schmidt <sm...@xs...> wrote:
From my book:
Given x = 123 and ptr_int = &x after that *ptr_int = 456 has been
executed.
What is the value of x?
I thought it was 123 as the pointer *ptr_int is a null pointer and can't
have any value.
This is how I see it:
main ( )
{
int x, *ptr_int;
*ptr_int = 456; /*This pointer is a null pointer at this moment right?
*/
x =123;
ptr_int = &x; /* As x is just set to 123 and the statement *ptr_int =
456;
was executed before I would think *ptr_int would be 123 */
}
----------
But the books says the value of *ptr_int at the end is 456. How come??
Thanks for your time,
Christian
-------------------------------------------------------
This SF.net email is sponsored by: ObjectStore.
If flattening out C++ or Java code to make your application fit in a
relational datab! ase is painful, don't do it! Check out ObjectStore.
Now part of Progress Software. http://www.objectstore.net/sourceforge
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
Thanx and Regards
Avik Ghose
Catch all the cricket action. Download Yahoo! Score tracker
|