Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » C Language » Pointers in C Programming, Definition, Types & Example

  • Gautama Buddha life story in Hindi, Buddha's history
    Gautama Buddha life story in Hindi, Buddha’s history Biography
  • What is Adjective in Hindi (विशेषण क्या है?)
    What is Adjective in Hindi (विशेषण क्या है?) Grammar
  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival
  • Top C++ programs for geeks
    Top C++ programs for geeks | C++ code examples Programs
  • Difference between Jenkins and TeamCity
    Difference between Jenkins and TeamCity, Maven, Octopus Differences
  • Spring Initializr for Spring Boot Project
    Spring Initializr for Spring Boot Project Spring Boot
  • Steps of Database Connectivity in Java
    Steps of Database Connectivity in Java Java
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography

Pointers in C Programming, Definition, Types & Example

Posted on December 7, 2021December 7, 2021 By admin No Comments on Pointers in C Programming, Definition, Types & Example
Pointers in C Programming, Definition, Types & Example

Pointers in C are variables that store the memory location, or memory address, of a data item to be held in memory rather than its value. A memory address is assigned to a pointer. Typically, this address refers to the memory location of some other variable. The first variable points to the second if one variable holds the address of another variable.

When a variable holds the address of the variable’s storage cell, we call it a pointer. The variable declared by the type is also recorded, as is the type specified by the name. The * symbol is used to declare a pointer variable.

Pointers may be valuable, powerful, and relatively painless tools as long as you make sure that they always refer to valid memory in your application.

Table of Contents

  • Use of Pointers in C
  • Declaration of Pointers in C
  • Pointer Operators in C
  • What are the various types of pointers in the C language?
    • Null Pointer
    • Void Pointer
  • Array of pointers in C programming
    • One-Dimensional array with pointer
    • Two-Dimensional array with pointer
  • Function Pointer in C
  • Pointer to Pointer in C (Double Pointer)

Use of Pointers in C

  • Memory is stored via pointers.
  • Memory information is sent via pointers.
  • Because data modification is done using addresses rather than pointers, the execution time is short.
  • When a function returns several values, pointers are used.
  • To make passing arrays and strings between functions easier.
  • For dynamic memory allocation we use pointer.

Declaration of Pointers in C

int *ptr;  /* The term *ptr is an int, it says. */

As a more typical, even less significant, and even more straightforward example:

int val = 1; /* An integer */
int *ptr;  /* A pointer-to-int */

There are two parts to a pointer variable.

  • Pointer operator
  • Address operator

*(asterisk) and a variable can be used to represent a pointer operator. The address of an integer datatype is stored in ipr, which is a pointer variable.

int *ptr;  /* A pointer-to-int */

A combination of &(ampersand) and a pointer variable is an address operator. The & operator should be used if you wish to return the variable’s memory address. Here, ptr holds the address of a.

ptr = &a;

If we combine the pointer operator and the address operator in one example, the best example of a pointer would be the following:

int a = 10; /* An integer variable */
int *ptr;   /* A pointer variable */
ptr = &a;   /* Pointer variable holds the address of an integer variable */
#include <stdio.h>
int main()
{
   int *ptr, a;
   
   a = 10;
   printf("Address of a: %p\n", &a);
   printf("Value of a: %d\n\n", a);  // 10
   
   ptr = &a;
   printf("Address of pointer ptr: %d\n", ptr);
   printf("Content of pointer ptr: %d\n\n", *ptr); // 10
   
   return 0;
}
Output:
Address of a: 0x7ffc7c5b72bc
Value of a: 10

Address of pointer ptr: 2086367932
Content of pointer ptr: 10

In the above program, if you compile and run repeatedly, the output of the address (address of a and ptr) may be different.

Pointer Operators in C

On pointers, we can use the following arithmetic operators.

OperatorNameExample
+Addition*sum = *n1 + *n2
–Subtraction*sub = *n1 – *n2
*Multiplication*multi = *n1 * *n2
/Division*div= *n1 / *n2
%Modulus*mod= *n1 % *n2

With pointer variables, you may use the increment (++) and decrement (–) operators, but the ++ operator increments the pointer but not by one, and the — operator decrements the pointer but not by one.

According to the data type, the increment and decrement will be done by the number of bytes.

You can also use the following comparison operators to compare two pointer variables:

  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
  • Equal to (==)
  • Not equal to (!=)

What are the various types of pointers in the C language?

The following are the eight categories of pointers:

  • Complex Pointer
  • Dangling Pointer
  • Far Pointer
  • Huge Pointer
  • Near Pointer
  • Null Pointer
  • Void Pointer
  • Wild Pointer

Null Pointer

When a null value is assigned to a pointer at the moment of declaration, it is nothing but null pointer. What is the value of null pointer? The value of a null pointer is always 0.

#include <stdio.h>
int main()
{
   int *p = NULL; // This is the null pointer
   printf("The value of p is: %d", p);
   return 0;
}
Output:
The value of p is: 0

When we don’t want to send any actual memory location in the function parameter, we can use this null pointer. In this scenario, we can pass a null pointer.

Void Pointer

The void pointer has the characteristic of not being associated with any data type. You can cast it to any type and it can hold the address of any type of element.

#include<stdio.h>
main()
{
  char ch='A';
  int i = 10;
  void *ptr;
  ptr = &ch;
  printf("\n%c", (char *)ch);
  ptr = &i;
  printf("\n%d", (int *)i);
}
Output:
A
10

Array of pointers in C programming

It is also possible to create an array in which several pointers of the same type can be kept.

#include<stdio.h>
main()
{
    int *arr[4];
    int i, a = 10, b = 20, c =30, d = 40;
    arr[0] = &a;
    arr[1] = &b;
    arr[2] = &c;
    arr[3] = &d;
    for( i = 0; i < 4; i++ ) 
    {
        printf("\n Value is %d ", *(arr[i]));
    }
}
Output:
Value is 10 
Value is 20 
Value is 30 
Value is 40 

One-Dimensional array with pointer

In a one-dimensional array, a pointer will contain the address of the first element of the array. In the example below the first element is arr[0] and the address of the first element is &arr[0] .

#include<stdio.h>
main()
{
    int arr[5] = {10, 20, 30, 40, 50};
    int i, *ptr;
    ptr = &arr[0]; /* Address of first element */
    for( i = 0; i < 4; i++ ) 
    {
        printf("\n Address is %d, Value is %d", ptr,  *ptr);
        ptr = ptr + 1;
    }
}
Output:
Address is 1725494832, Value is 10
Address is 1725494836, Value is 20
Address is 1725494840, Value is 30
Address is 1725494844, Value is 40

Two-Dimensional array with pointer

#include<stdio.h>
main()
{
    int arr[3][2] = {{10,20}, {30, 40}, {50, 60}};
    int i, j, *ptr;
    ptr = &arr[0][0];
    for( i = 0; i < 3; i++ ) 
    {
        for (j = 0; j < 2; j++) 
        {
            printf(" %d", *(ptr+ i*2 +j));
        }
        printf("\n");
    }
}
Output:
 10 20
 30 40
 50 60

Function Pointer in C

The address of a function can be stored in a C program. In C, the idea of storing the address of a function is known as function pointer.

#include<stdio.h>
void addition(int n1, int n2){
    printf("\n%d", n1 + n2);
}
void main()
{
	void (*fp) (int a,int b); // This is function pointer
	fp = addition; //Here pointer stores the function address
	(*fp) (10,20); // Passing value to the function pointer
    fp (100,20); // calling function
}
Output:
30
120

Pointer to Pointer in C (Double Pointer)

In C programming we can create a pointer which will hold the address of another pointer. It is also called double pointer.

int var = 10;
int *ptr1;
int **ptr2;
ptr = &var;
ptr = &ptr1;
#include<stdio.h>
void main()
{
  int var = 10;
  int *ptr1;
  int **ptr2;
  ptr1 = &var;
  ptr2 = &ptr1;
  printf("\n%d",var);
  printf("\n%d",*ptr1);
  printf("\n%d",**ptr2);
}   
Output:
10
10
10

In conclusion, In this article we have covered the basics of pointer in C programming and pointer types, examples using pointer, pointer array, function pointer and pointer to pointer.

Hope you liked this article and this article can help you in the example and interview.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

C Language Tags:Array using pointer program in C, How do two dimensional arrays use pointers?, How do you declare a pointer array in 2D?, How to declare two dimensional array using pointers in C?, Pointer to an array

Post navigation

Previous Post: Function in C Language | Recursive function with example
Next Post: Structure in C program with example | Nested structure in C

More Related Articles

Constants and Operators in C Language Constants and Operators in C Language C Language
Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language
Structure in C program with example | Nested structure in C Structure in C program with example | Nested structure in C C Language
Function in C Language | Recursive function with example Function in C Language | Recursive function with example C Language
Storage classes in C with examples (auto, extern, static, register) Storage classes in C with examples (auto, extern, register, static) C Language
String Array in C | What is an array of string | String Functions String Array in C | What is an array of string | String Functions C Language

Related Posts

  • Constants and Operators in C Language
    Constants and Operators in C Language C Language
  • Linked List in Data Structure and Algorithm, Types, Operations
    Linked List in Data Structure and Algorithm, Types, Operations C Language
  • Structure in C program with example | Nested structure in C
    Structure in C program with example | Nested structure in C C Language
  • Function in C Language | Recursive function with example
    Function in C Language | Recursive function with example C Language
  • Storage classes in C with examples (auto, extern, static, register)
    Storage classes in C with examples (auto, extern, register, static) C Language
  • String Array in C | What is an array of string | String Functions
    String Array in C | What is an array of string | String Functions C Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • C Home
  • Constants and Operators
  • Statements in C Language
  • Array in C
  • String Array in C
  • Functions in C
  • Pointers in C
  • Structure in C
  • Storage Classes
  • File Handling in C
  • Top C Programs

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • Gautama Buddha life story in Hindi, Buddha's history
    Gautama Buddha life story in Hindi, Buddha’s history Biography
  • What is Adjective in Hindi (विशेषण क्या है?)
    What is Adjective in Hindi (विशेषण क्या है?) Grammar
  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival
  • Top C++ programs for geeks
    Top C++ programs for geeks | C++ code examples Programs
  • Difference between Jenkins and TeamCity
    Difference between Jenkins and TeamCity, Maven, Octopus Differences
  • Spring Initializr for Spring Boot Project
    Spring Initializr for Spring Boot Project Spring Boot
  • Steps of Database Connectivity in Java
    Steps of Database Connectivity in Java Java
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved