ra*******@gmail.com wrote:
hello,
I am trying following code that help me to understand difference
between what happens when value is assigned to pointer or reference but
it has some errors how to remove that?
#include <stdio.h>
void main()
{
int i;
int &k=i;
k++;
printf("val of i is %d\n",i);
k=200;
printf("val of i is %d\n",i);
int *p=&i;
p++;
printf("val of p is %d\n",p);
printf("val of i is %d\n",i);
}
#include <stdio.h>
int /* mha: was 'void' */ main(void)
{
int i;
int *k /* mha: was '&k' */ = &i; /* mha: OP's initialization was
'=i', which both assigns an
int to a pointer and uses
the uninitialized value of i
*/
k++; /* mha: unchanged, since k is never
dereferenced (thank goodness) */
printf("val of i is %d\n", i);
k = (int *) 200; /* mha: cast introduced, but otherwise
unchanged, since k is never
dereferenced (thank goodness) */
printf("val of i is %d\n", i);
{
/* mha: block introduced because of what would be a C89 (C90)
error of a misplaced declaration. I could have moved the
declaration to the beginning of the enclosing block instead. */
int *p = &i;
p++; /* mha: unchanged, since p is never
dereferenced (thank goodness) */
printf("val of p is %p\n", (void *) p); /* mha: was
'printf("val of p is %d\n", p);' */
}
printf("val of i is %d\n", i);
return 0; /* mha: added */
}