Thread: [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
|
|
From: <or...@vp...> - 2003-05-27 20:13:35
|
Christian Schmidt wrote:
> Look at and perhaps run the commented source code:
There are a few errors about your code. You should enable -Wall, and
correct ALL WARNINGS ALWAYS! Warnings are most often needs to be treated
as errors! They show poor desing. I will mark the errors inside the
quoted code:
> #include <stdio.h>
>
> main ( )
> {
> double flt_num, *flt_ptr;
>
> flt_num = 123,45;
/* Use a period "." for floating-point values instead of comma "," */
>
> /* 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;
/* Use a period "." for floating-point values instead of comma "," */
>
> /* 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! */
/* This line has an error. Try this:
printf ("flt_ptr = %p.\n",flt_ptr);
You should print it out as a pointer, NOT as a double. The compiler
will warn you about this.
*/
>
> printf ("The adress of flt_ptr is %p.\n",&flt_ptr);
>
> printf ("*flt_ptr = %f.\n", *flt_ptr);
>
> return 0;
Remember: add -Wall flag for the compiler ALWAYS!
--
Greetings,
E.
|
|
From: Smurf, P. <smu...@xs...> - 2003-05-27 20:29:07
|
> /* Use a period "." for floating-point values instead of comma "," */ I was thinking about this actually before I entered it. But I figured as I'm using a Win2000 system with a Dutch regional setting the compiler would figure out what I mean. But thanks for the warning. > Remember: add -Wall flag for the compiler ALWAYS! > I didn't know about this. How do I enable this from DevCPP? What I did is: Tools --> Compiler Options --> Add the following commands when calling the compiler (-Wall). But nothing happens. |
|
From: Per W. <pw...@ia...> - 2003-05-27 22:45:22
|
When printing the value of a pointer, do:
printf("The pointer has value %p\n",flt_ptr);
and when printing the value it points to:
printf("It points to value %f\n",*flt_ptr);
While a high warning level in the compiler - some nice compilers such as
gcc (MinGW) - do parse the formatting of printf() and scanf() functions,
most compilers doesn't help with variadic functions. Always make sure
yourself that you really know what type you expect to print out, and
uses the correct formatting character - and sometimes also typecast the
parameter.
/Per W
On Tue, 27 May 2003, Christian Schmidt wrote:
> 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
>
|