"Miks" <> writes:
> I'm using Turbo c 2.01, Borland International
>
> #include <stdio.h>
> main()
> {
> float a = 2.0;
> int *p;
> p = a;
> printf("%u",p);
> getch();
> }
"main()" should be "int main(void)".
The assignment
p = a;
attempts to assign a float value to a pointer. This is a constraint
violation, requiring a diagnostic.
If it were changed to
p = &a;
it would still be a constraint violation; you can convert one pointer
type to another, but not without a cast.
In the statement
printf("%u", p);
the "%u" format causes printf to expect an unsinged int; passing an
int* instead invokes undefined behavior. It also fails to terminate
the program's output with a new-line. The correct statement would be
printf("%p\n", (void*)p);
There is no getch() function in standard C, and you've failed to
#include any header that declares it. It's probably not necessary
anyway.
The program should end with a "return 0;".
I'm not going to try to come up with a correct version of the program;
there's not enough there to figure out what it's supposed to do.
(Somebody suggested that a debugger would be useful in understanding
this code. I don't see how. The code is incorrect; even if you could
compile it, your time would be better spent correcting it than
figuring out exactly how it behaves incorrectly. Undefined behavior
is undefined behavior.)
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.