Suppose there is a pointer variable *p points the variable a then we can write:
int c, *pc;
// pc is address but c is not
pc = c; // Error
// &c is address but *pc is not
*pc = &c; // Error
// both &c and pc are addresses
pc = &c;
// both c and *pc values
*pc = c;
- pc = c means “pc” is address but “c” is not address. Here “c” is a variable.
- *pc = &c means “*pc” is variable that points the value of “c” not address.
- pc = &c means both “pc” and “c” are addresses.
- *pc = c means both “*pc” and “c” refers to values.
So when beginner’s writing code in pointer they have some confusion arises in mind:
#include <stdio.h>
int main()
{
int c = 5;
int *p = &c;
printf("%d", *p);
return 0;
}
Output:
*p = 5
So, why we didn’t get any error message when writing int *p = &c?
Because we can’t get error,
int *p = &c;
is equivalent to
int *p;
p = &c;
In both the cases we are using pointer “p” and assigning the address “(&c)” on it.
So, to avoid the confusion we are using the statement like this.
int *p = &c;
If you find any issue and interested to rewrite it then contact us.