Common Mistakes When Working With Pointer

Share Your Love

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;
  1. pc = c means “pc” is address but “c” is not address. Here “c” is a variable.
  2. *pc = &c means “*pc” is variable that points the value of “c” not address.
  3. pc = &c means both “pc” and “c” are addresses.
  4. *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.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter