Re: [Dev-C++] Another pointer question (I think its an compiler issue)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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.
|