Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!hp-pcd!hpcvia!brianh From: brianh@hpcvia.CV.HP.COM (brian_helterline) Newsgroups: comp.lang.c Subject: Re: Novice MicroSoft C5.1 question Message-ID: <31530011@hpcvia.CV.HP.COM> Date: 26 Jul 90 15:32:20 GMT References: <9609@tekigm2.MEN.TEK.COM> Organization: Hewlett-Packard Co., Corvallis, Oregon Lines: 51 > 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@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 >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 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