Professional Documents
Culture Documents
Common Pointer Mistakes
Common Pointer Mistakes
Correction:
1. ptr is pointer variable which is used to store the address of the
variable.
2. Pointer Variable ptr contain the address of the variable of type
int as we have declared pointer variable with data type int.
3. We are assigning value of Variable to the Pointer variable, instead
of Address.
4. In order to resolve this problem
variable to pointer variable
we
should
assign
address
of
ptr = &m ;
pointed
by
to
&m ;
printf("%d",ptr);
In the above example, You will not get any compiler or run time error
but it is considered as common mistake by novice user to de-reference
pointer variable without using asterisk (*)
int *ptr,m;
ptr = &m;
Best practice while using pointer is to initialize pointer before using
it.
We can assign any valid address to pointer variable but if you assign
the address of un-initialized variable to pointer then it will print
garbage value while de-referencing it.
char str1[10],str2[10];
char *ptr1 = str1;
char *ptr2 = str2;
if(ptr1 > ptr2)
{
....
....
}
.....
// Error Here