
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
Constant Pointers & Pointer to Constant in C
In C, a pointer is a variable that stores the memory address of another variable, and the const keyword is used to define a variable or pointer whose value cannot be changed once initialized. When we combine pointers with const keyword, we can control two things −
- Whether the address stored in the pointer can change.
- Whether the value stored at that address can change.
In this chapter, we will look at the three main variations of constant pointers −
Constant Pointer
A constant pointer means the pointer itself is constant. Once it is initialized to point to a memory location, it cannot point to a different location, but the value stored at that location can still be changed.
Following is the syntax of a constant pointer −
data_type *const pointer_name = &variable;
In this syntax, data_type is the type of data the pointer points to, *const makes the pointer itself constant, pointer_name is the pointer's name, and &variable assigns it the memory address of a variable.
Example of Constant Pointer
In this example, we declare a constant pointer ptr and initialize it with the address of variable x. Then, we change the value of x using ptr and we print its value.
#include <stdio.h> int main() { int x = 10; int y = 20; int *const ptr = &x; // constant pointer to int printf("Value of x: %d\n", *ptr); *ptr = 15; // can change the value at address printf("Modified value of x: %d\n", *ptr); // ptr = &y; // changing pointer address is not allowed return 0; }
Shown below is the output of the above program, which shows that the pointer remains fixed to x, but the value of x can be updated.
Value of x: 10 Modified value of x: 15
Example of a Constant Pointer Error
Here's an example where we declare a constant pointer ptr and initialize it with the address of variable x. Then, we try to make it point to the address of variable y. This will give an error because a constant pointer cannot point to another memory location once it has been initialized.
#include <stdio.h> int main() { int x = 10; int y = 20; int *const ptr = &x; // constant pointer to int printf("Value of x: %d\n", *ptr); // Attempting to change the pointer to point to y ptr = &y; // cannot change the address of a constant pointer return 0; }
You can see the error below, indicating that we cannot change the address of a constant pointer.
error: assignment of read-only variable 'ptr'
Pointer to Constant
A pointer to constant means the value it points to cannot be changed, but the pointer itself can point to different memory addresses (or variables).
Following is the syntax for pointer to constant −
const data_type *pointer_name = &variable; data_type const *pointer = &variable;
In this syntax, const data_type or data_type const means the pointer points to a constant value, pointer_name is the name of the pointer, and &variable assigns it the address of a variable.
Example of Pointer to Constant
In this example, we declare a pointer ptr that points to a constant value and assign it the address of variable a. Then, we make the pointer point to a different address of variable b and print its value.
#include <stdio.h> int main() { int a = 5; int b = 30; const int *ptr = &a; // pointer to constant int printf("Value of a: %d\n", *ptr); // *ptr = 10; //we cannot modify value through pointer ptr = &b; // canging pointer address printf("Now pointing to b: %d\n", *ptr); return 0; }
Following is the output of the above program, showing the same pointer pointing to different variables.
Value of a: 5 Now pointing to b: 30
Example of a Pointer to Constant Error
Here's an example where we declare a pointer to constant ptr and initialize it with the address of variable a. Then, we try to change the value of a through the pointer. This will give an error because a pointer to constant does not allow modifying the value it points to.
#include <stdio.h> int main() { int a = 5; const int *ptr = &a; // pointer to constant int printf("Value of a: %d\n", *ptr); // we cnnot modify value through pointer to constant *ptr = 10; return 0; }
Below you can see the output, which shows an error indicating that we cannot modify the value through a pointer to constant.
error: assignment of read-only location '*ptr'
Constant Pointer to Constant
A constant pointer to a constant is a pointer that cannot change its memory address, and the value stored at that memory address also cannot be changed. Both actions are restricted, so we can only read the value, nothing else.
Following is the syntax for a constant pointer to a constant −
const data_type *const pointer_name = &variable;
In this syntax −
- const data_type indicates that the value at the memory location cannot be changed through the pointer.
- *const pointer_name means the pointer itself cannot point to any other memory address after initialization.
- &variable assigns the pointer to the memory address of the variable.
Example of Constant Pointer to Constant
In this example, we declare a constant pointer to constant ptr and assign it the memory address of variable a. We then print the value of a using the pointer.
#include <stdio.h> int main() { int a = 10; const int *const ptr = &a; // constant pointer to constant printf("Value of a: %d\n", *ptr); // *ptr = 15; // we cannot modify value // ptr = &b; // we annot change pointer location return 0; }
Following is the output of the above program −
Value of a: 10
Example of Constant Pointer to Constant Error
In this example, we declare a constant pointer to constant ptr and initialize it with a variable a. Then, we try to modify the value through the pointer and also try to make the pointer point to another variable. Both operations are not allowed and will result in a compiler error.
#include <stdio.h> int main() { int a = 10; int b = 20; const int *const ptr = &a; // constant pointer to constant printf("Value of a: %d\n", *ptr); // *ptr = 15; // we cnnot modify value // ptr = &b; // we cannot change pointer address return 0; }
Below you can see the output showing the errors −
Value of a: 10
Difference between Constant Pointer Types
The following table shows the differences between a constant pointer, a pointer to constant, and a constant pointer to constant.
Variation | Definition | Can Change Address? | Can Change Value? | Example Syntax |
---|---|---|---|---|
Constant Pointer | A pointer whose address is fixed, but the value at that address can be modified. | No | Yes | int *const p = &x; |
Pointer to Constant | A pointer that can point to different addresses, but cannot modify the value at the pointed location. | Yes | No | const int *p = &x; |
Constant Pointer to Constant | A pointer whose address is fixed, and the value at that address cannot be modified. | No | No | const int *const p = &x; |
Conclusion
In this chapter, we covered constant pointers and pointers to constant in C. Constant pointers fix the address but allow changing the value, pointers to constant allow changing the address but not the value, and constant pointers to constant restrict both.