Novice MicroSoft C5.1 question
brian_helterline
brianh at hpcvia.CV.HP.COM
Fri Jul 27 01:32:20 AEST 1990
> Could anyone explain the unexpected output of these two programs
>to me please?? Being a beginning C person, it's entirely likely that
>I have a simple syntax error or my concept of pointers is wrong. I
>am using MicroSoft C5.1 on a '386 at 16MHz. I posted this once before,
>but received no help; not even to tell me how stupid I am and that
>such a simple question is beneath response. As to the question:
>Running this:
[ example deleted ]
>#include <stdio.h>
>main()
>{
> int x=100;
> int *y;
> y= &x;
> printf("\nx:%d &x:%u",x,&x);
> printf(" y(add of x):%u &y(add of y):%u *y(value of x):%d\n",y,&y,*y);
>}
>
>Produces the unexpected output:
>x:100 &x:11094 y(add of x):11094 &y(add of y):7647 *y(value of x):11090
>I do not understand why there is a difference. Could some nice soul explain
>this in a way a novice could understand?? Thanks in advance.
>----------
The problem is that you are printing out an address using %u
which expects sizeof( unsigned int ) bytes on the stack but
you are shoving an address onto the stack. To print out an
address, use %p ( MSC RunTime Ref pg 459 ). Also, it is
important to know what memory model you are using. The %p
requires a far pointer. The code below will work with any
memory model. (I did not test this code :)
#include <stdio.h>
main()
{
int x=100;
int *y;
y= &x;
printf("\nx:%d &x:%p",x,(int far *)&x);
printf(" y(add of x):%p &y(add of y):%p *y(value of x):%d\n",(int far *)y,
(int far *)&y, *y);
}
Hope this helps.
-Brian
More information about the Comp.lang.c
mailing list