Pointers In C – With Examples

Share Your Love

Here we are discuss how pointer works on different types? Pointer is a vital topic in C programming language. Every programmer should follow the basic rules of pointer.

What is a pointer?

The pointer is a variable that stores or points address of another variable. The pointer is a variable that might be belonging to any of the data types means int, float, char, long etc.

Syntax Of Pointer:

data-type *variable-name;

Example:

int *p;  char *p;

Here “*” denotes that p is a pointer variable not a normal variable.

Example:

#include<stdio.h>
int main()
{
    int a=2,*p;
    float b=3, *q;
    char c='a', *r;
    p = &a;
    q = &b;
    r = &c;
    printf("a=%d\t,*p=%d\n",a,*p);
    printf("b=%f\t,*q=%f\n",b,*q);
    printf("c=%c\t,*r=%c\n",c,*r);
    return 0;
}
#Output
a=2   *p=2
b=3.00000   *q=3.00000
c=a   *r=a

A pointer in C programming is called a Dynamic Memory Allocation means to allocate memory at runtime.

Some Key Points Remember In Pointer C Programming

  1. Normal Variables are stores the value whereas pointer variables are always stores address.
  2. The content of the pointer variable is always a whole number i.e. address.
  3. Always pointer variables are initialized with null, means *p = null.
  4. The value of a null pointer is 0.
  5. “&” symbol is used the reference variable in “C” Programming.
  6. “*” symbol is used to refer the value of pointer variable that pointer points.
  7. If a pointer assigning a “null” means points to nothing.
  8. Two pointers can be subtracted to know how many elements are remaining between these 2 pointers.
  9. But pointer addition, multiplication and divisions are not allowed.
  10. The size of the pointer depends on the type of variable, that pointer points. Means see an Example:
#include<stdio.h>
int main()
{
   int a=2,*p;
   p=&a;
   printf("a=%d\t,*p=%d",a,*p);
   return 0;
}
#Output
a=2   *p=2

Remember:

The pointer always declare the same type the variable declare.

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