Novice MicroSoft C5.1 question

Richard R Prince richardp at tekigm2.MEN.TEK.COM
Thu Jul 26 09:32:10 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:

#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);
 printf("  &y(add of y):%u",&y);
 printf("  *y(value of x):%d\n",*y);
}

produces the output I would expect:
x:100  &x:11094  y(add of x):11094  &y(add of y):11090  *y(value of x):100

But running this:

#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.



More information about the Comp.lang.c mailing list