Code:
#include<stdio.h>
main()
{
int x=5, px = &x;
float y=5.0, py = &y;
printf("%d %ld\n",x,(long) px);
printf("%d %ld\n",x+1,(long) ( px+1));
printf("%f %ld\n",y,(long) py);
printf("%f %ld\n", y+1, (long) (py+1));
}
So, to use pointers you declare it like *ptr, but then use it without the *?
Because in this case it is supposed to display &x and &y in printf, so I got to do that.
But if I write the code like this:
Code:
#include<stdio.h>
main()
{
int x=5, *px = &x;
float y=5.0, *py = &y;
printf("%d %ld\n",x,(long) *px);
printf("%d %ld\n",x+1,(long) ( *px+1));
printf("%f %ld\n",y,(long) *py);
printf("%f %ld\n", y+1, (long) (*py+1));
}
It displays x and y, respectively.
So, if I write *ptr, it refers to the value of the variable it points to, and if I write ptr, it refers to &variable?
But writing *ptr = x causes an error.
So, maybe writing *ptr= &x causes ptr to have the value of &x and *ptr to have the value of x? Is that it? Am I right?