- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- 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
- 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
- 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 - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Dereference Pointer
![]() Share with a Friend |
C Programming - C Dereference Pointer
C Dereference Pointer
Dereferencing a pointer means accessing the value that the pointer is pointing to. In C, dereferencing is done using the * operator, which allows you to access the memory location the pointer is pointing to and work with the value stored there.
- Syntax for Dereferencing a Pointer:
C
*ptr
Here:
- ptr is a pointer variable.
- The * is the dereference operator.
Dereferencing the pointer gives you access to the value stored at the memory location that ptr is pointing to.
- Example of Dereferencing a Pointer:
C
#include <stdio.h>
int main() {
int num = 10; // Declare an integer variable
int *ptr = # // Declare a pointer and assign it the address of 'num'
printf("Value of num: %d\n", num); // Output the value of 'num'
printf("Value using pointer: %d\n", *ptr); // Dereference 'ptr' to get the value of 'num'<
return 0;
}
Explanation:
- num is an integer variable with a value of 10.
- ptr is a pointer that holds the address of num (i.e., ptr = &num).
- The dereference operator * is used to access the value stored at the memory location ptr is pointing to, which is 10 in this case.
Output:
Value of num: 10
Value using pointer: 10
- Dereferencing Pointers:
- Accessing value: When you dereference a pointer, you get the value stored at the memory location the pointer is pointing to.
- Modifying value: Dereferencing can also be used to modify the value at the memory location the pointer is pointing to.
- Modifying the Value Using Dereferencing:
C
#include <stdio.h>
int main() {
int num = 5;
int *ptr = # // Pointer 'ptr' holds the address of 'num'
// Dereference the pointer to modify the value of 'num'
*ptr = 20; // This changes the value of 'num' to 20
printf("New value of num: %d\n", num); // Output the new value of 'num'
return 0;
}
Explanation:
- Initially, num is 5.
- The pointer ptr is assigned the address of num.
- By dereferencing the pointer *ptr, we assign a new value (20) to num through the pointer.
- The change is reflected in the value of num.
Output:
New value of num: 20
- Common Mistakes When Dereferencing Pointers:
- Dereferencing a NULL Pointer: If a pointer is NULL (i.e., it doesn't point to a valid memory location), dereferencing it will cause undefined behavior, often resulting in a segmentation fault.
C
int *ptr = NULL;
printf("%d", *ptr); // Dereferencing a NULL pointer - undefined behavior
Solution: Always check if the pointer is not NULL before dereferencing.
C
if (ptr != NULL) {
printf("%d", *ptr);
} else {
printf("Pointer is NULL\n");
}
- Dereferencing Uninitialized Pointers: If a pointer is not initialized to a valid memory address, dereferencing it can lead to accessing invalid memory.
C
int *ptr; // Uninitialized pointer
printf("%d", *ptr); // Dereferencing an uninitialized pointer - undefined behavior
Solution: Always initialize pointers before dereferencing.
C
int num = 10;
int *ptr = # // Pointer initialized to the address of 'num'
printf("%d", *ptr); // Safe dereferencing
- Dereferencing a Pointer After Freeing Memory: After a pointer is freed using free(), the memory is no longer valid. Dereferencing such a pointer leads to undefined behavior.
C
int *ptr = (int*)malloc(sizeof(int));
*ptr = 5;
free(ptr);
printf("%d", *ptr); // Dereferencing after freeing memory - undefined behavior
Solution: After freeing memory, set the pointer to NULL to avoid dereferencing a dangling pointer.
C
free(ptr);
ptr = NULL; // Pointer is now safe to use
- Key Points to Remember:
- Dereferencing is the process of accessing the value at the address a pointer holds.
- Use the dereference operator * to retrieve or modify the value a pointer points to.
- Always ensure that a pointer is not NULL or uninitialized before dereferencing it.
- After freeing dynamically allocated memory, always set the pointer to NULL to avoid dangling pointers.
Summary:
Dereferencing a pointer in C allows you to access and manipulate the value stored at the memory location the pointer is pointing to. This is done using the * operator. It is important to handle pointers carefully to avoid errors like dereferencing NULL or uninitialized pointers, and to prevent memory access violations.
