Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Pointer Arithmetics in C with Examples
Next article icon

C Pointers

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of low-level memory manipulation in C.

Declare a Pointer

A pointer is declared by specifying its name and type, just like simple variable declaration but with an asterisk (*) symbol added before the pointer’s name.

C
data_type* name 

Here, data_type defines the type of data that the pointer is pointing to. An integer type pointer can only point to an integer. Similarly, a pointer of float type can point to a floating-point data, and so on.

Example:

C
int *ptr; 

In the above statement, pointer ptr can store the address of an integer. It is pronounced as pointer to integer.

Initialize the Pointer

Pointer initialization means assigning some address to the pointer variable. In C, the (&) addressof operator is used to get the memory address of any variable. This memory address is then stored in a pointer variable.

Example:

C
int var = 10;   // Initializing ptr int *ptr = &var; 

In the above statement, pointer ptr store the address of variable x which was determined using address-of operator (&).

Note: We can also declare and initialize the pointer in a single step. This is called pointer definition.

Dereference a Pointer

Accessing the pointer directly will just give us the address that is stored in the pointer. For example,

C++
//Driver Code Starts{ #include <stdio.h>  int main() {     int var = 10;          // Store address of var variable     int* ptr = &var;      //Driver Code Ends }      // Directly accessing ptr     printf("%d", ptr);  //Driver Code Starts{          return 0; } //Driver Code Ends } 


Output

0x7fffa0757dd4

This hexadecimal integer (starting with 0x) is the memory address.

We have to first dereference the pointer to access the value present at the memory address. This is done with the help of dereferencing operator(*) (same operator used in declaration).

C
//Driver Code Starts{ #include <stdio.h>  int main() {     int var = 10;          // Store address of var variable     int* ptr = &var;      //Driver Code Ends }      // Dereferencing ptr to access the value     printf("%d", *ptr);  //Driver Code Starts{          return 0; } //Driver Code Ends } 

Output
10

Note: Earlier, we used %d for printing pointers, but C provides a separate format specifier %p for printing pointers.

Size of Pointers

The size of a pointer in C depends on the architecture (bit system) of the machine, not the data type it points to.

  • On a 32-bit system, all pointers typically occupy 4 bytes.
  • On a 64-bit system, all pointers typically occupy 8 bytes.

The size remains constant regardless of the data type (int*, char*, float*, etc.). We can verify this using the sizeof operator.

C
//Driver Code Starts{ #include <stdio.h>  int main() { //Driver Code Ends }      int *ptr1;     char *ptr2;          // Finding size using sizeof()     printf("%zu ", sizeof(ptr1));     printf("%zu", sizeof(ptr2));  //Driver Code Starts{          return 0; } //Driver Code Ends } 

Output
8 8

The reason for the same size is that the pointers store the memory addresses, no matter what type they are. As the space required to store the addresses of the different memory locations is the same, the memory required by one pointer type will be equal to the memory required by other pointer types.

Note: The actual size of the pointer may vary depending on the compiler and system architecture, but it is always uniform across all data types on the same system.

Special Types of Pointers

There are 4 special types of pointers that used or referred to in different contexts:

NULL Pointer

The NULL Pointers are those pointers that do not point to any memory location. They can be created by assigning NULL value to the pointer. A pointer of any type can be assigned the NULL value.

C
//Driver Code Starts{ #include <stdio.h>  int main() { //Driver Code Ends }      // Null pointer     int *ptr = NULL;  //Driver Code Starts{          return 0; } //Driver Code Ends } 

NULL pointers are generally used to represent the absence of any address. This allows us to check whether the pointer is pointing to any valid memory location by checking if it is equal to NULL.

Void Pointer

The void pointers in C are the pointers of type void. It means that they do not have any associated data type. They are also called generic pointers as they can point to any type and can be typecasted to any type.

C
//Driver Code Starts{ #include <stdio.h>  int main() { //Driver Code Ends }      // Void pointer     void *ptr;  //Driver Code Starts{          return 0; } //Driver Code Ends } 

Wild Pointers

The wild pointers are pointers that have not been initialized with something yet. These types of C-pointers can cause problems in our programs and can eventually cause them to crash. If values are updated using wild pointers, they could cause data abort or data corruption.

C
//Driver Code Starts{ #include <stdio.h>  int main() {  //Driver Code Ends }      // Wild Pointer     int *ptr;  //Driver Code Starts{          return 0; } //Driver Code Ends } 

Dangling Pointer

A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. Such a situation can lead to unexpected behavior in the program and also serve as a source of bugs in C programs.

C
//Driver Code Starts{ // C program to demonstrate Deallocating a memory pointed by // ptr causes dangling pointer #include <stdio.h> #include <stdlib.h>  int main() { //Driver Code Ends }      int* ptr = (int*)malloc(sizeof(int));      // After below free call, ptr becomes a dangling pointer     free(ptr);     printf("Memory freed ");      // removing Dangling Pointer     ptr = NULL;  //Driver Code Starts{      return 0; }  //Driver Code Ends } 

Output
Memory freed 

C Pointer Arithmetic

The pointer arithmetic refers to the arithmetic operations that can be performed on a pointer. It is slightly different from the ones that we generally use for mathematical calculations as only a limited set of operations can be performed on pointers. These operations include:

  • Increment/Decrement
  • Addition/Subtraction of Integer
  • Subtracting Two Pointers of Same Type
  • Comparing/Assigning Two Pointers of Same Type
  • Comparing/Assigning with NULL

C Pointers and Arrays

In C programming language, pointers and arrays are closely related. An array name acts like a pointer constant. The value of this pointer constant is the address of the first element. For example, if we have an array named val, then val and &val[0] can be used interchangeably.

If we assign this value to a non-constant pointer to array of the same type, then we can access the elements of the array using this pointer. Not only that, as the array elements are stored continuously, we can use pointer arithmetic operations such as increment, decrement, addition, and subtraction of integers on pointer to move between array elements.

This concept is not limited to the one-dimensional array, we can refer to a multidimensional array element as well using pointers.

Constant Pointers

In constant pointers, the memory address stored inside the pointer is constant and cannot be modified once it is defined. It will always point to the same memory address.

Example:

C
//Driver Code Starts{ #include <stdio.h>  int main() { //Driver Code Ends }      int a = 90;     int b = 50;      // Creating a constant pointer     int* const ptr = &a;          // Trying to reassign it to b     ptr = &b;  //Driver Code Starts{      return 0; } //Driver Code Ends } 


Output

solution.c: In function ‘main’: solution.c:11:9: error: assignment of read-only variable ‘ptr’    11 |     ptr = &b;       |         ^

We can also create a pointer to constant or even constant pointer to constant. Refer to this article to know more – Constant pointer, Pointers to Constant and Constant Pointers to Constant

Pointer to Function

A function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs.

Example:

C
//Driver Code Starts{ #include <stdio.h>  //Driver Code Ends }  int add(int a, int b) {     return a + b; }  int main() {        // Declare a function pointer that matches   	// the signature of add() fuction     int (*fptr)(int, int);      // Assign address of add()     fptr = &add;      // Call the function via ptr     printf("%d", fptr(10, 5));  //Driver Code Starts{      return 0; } //Driver Code Ends } 

Output
15

Multilevel Pointers

In C, we can create multi-level pointers with any number of levels such as – ***ptr3, ****ptr4, ******ptr5 and so on. Most popular of them is double pointer (pointer to pointer). It stores the memory address of another pointer. Instead of pointing to a data value, they point to another pointer.

Example:

C
//Driver Code Starts{ #include <stdio.h>  int main() { //Driver Code Ends }      int var = 10;        // Pointer to int     int *ptr1 = &var;        // Pointer to pointer (double pointer)     int **ptr2 = &ptr1;        // Accessing values using all three     printf("var: %d ", var);               printf("*ptr1: %d ", *ptr1);     printf("**ptr2: %d", **ptr2);  //Driver Code Starts{      return 0; } //Driver Code Ends } 

Output
var: 10 *ptr1: 10 **ptr2: 10

Uses of Pointers in C

The C pointer is a very powerful tool that is widely used in C programming to perform various useful operations. It is used to achieve the following functionalities in C:

  • Pass Arguments by Pointers
  • Accessing Array Elements
  • Return Multiple Values from Function
  • Dynamic Memory Allocation
  • Implementing Data Structures
  • In System-Level Programming where memory addresses are useful.
  • To use in Control Tables.

Advantages of Pointers

Following are the major advantages of pointers in C:

  • Pointers are used for dynamic memory allocation and deallocation.
  • An Array or a structure can be accessed efficiently with pointers
  • Pointers are useful for accessing memory locations.
  • Pointers are used to form complex data structures such as linked lists, graphs, trees, etc.
  • Pointers reduce the length of the program and its execution time as well.

Issues with Pointers

Pointers are vulnerable to errors and have following disadvantages:

  • Memory corruption can occur if an incorrect value is provided to pointers.
  • Pointers are a little bit complex to understand.
  • Pointers are majorly responsible for memory leaks in C.
  • Accessing using pointers are comparatively slower than variables in C.
  • Uninitialized pointers might cause a segmentation fault.
  • Quiz on Pointer Basics
  • Quiz on Advanced Pointer


Next Article
Pointer Arithmetics in C with Examples
author
kartik
Improve
Article Tags :
  • C Language
  • C-Pointers
  • Pointers
Practice Tags :
  • Pointers

Similar Reads

  • C Pointers
    A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
    10 min read
  • Pointer Arithmetics in C with Examples
    Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
    10 min read
  • Applications of Pointers in C
    Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
    4 min read
  • Passing Pointers to Functions in C
    Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po
    2 min read
  • C - Pointer to Pointer (Double Pointer)
    In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer. Let's take a look a
    5 min read
  • Chain of Pointers in C with Examples
    Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a v
    5 min read
  • Function Pointer in C
    In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
    6 min read
  • How to Declare a Pointer to a Function?
    A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming. In this article, we
    2 min read
  • Pointer to an Array | Array Pointer
    A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements. Example: [GFGTABS] C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int
    5 min read
  • Difference between constant pointer, pointers to constant, and constant pointers to constants
    In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences