- 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 Pointers
![]() Share with a Friend |
C Programming - C Pointers
Pointers in C
In C, pointers are variables that store the memory address of another variable. They are an essential feature of the language and allow for dynamic memory allocation, efficient handling of arrays and strings, and manipulation of data.
A pointer provides a way to indirectly access data by holding the address of a variable, rather than the value itself.
Key Concepts:
- Memory Address: Every variable in C has a memory address where it is stored. A pointer holds the address of a variable.
- Dereferencing: Dereferencing a pointer means accessing the value stored at the memory address the pointer is pointing to.
- Pointer Arithmetic: You can perform arithmetic operations on pointers, such as incrementing and decrementing, to navigate through memory locations.
Syntax:
- Declaration of Pointers:
C
data_type *pointer_name;
Here, pointer_name is a pointer variable that will store the address of a variable of type data_type.
- Initializing a Pointer:
C
pointer_name = &variable;
The & operator is used to get the memory address of a variable.
- Dereferencing a Pointer:
C
*pointer_name
The * operator is used to access the value stored at the address the pointer is pointing to.
Example of Pointers in C:
C
#include <stdio.h>
int main() {
int num = 10;
int *ptr = # // Pointer 'ptr' stores the address of 'num'
// Printing the value of 'num' using the pointer
printf("Value of num: %d\n", *ptr); // Dereferencing the pointer
// Printing the address of 'num'
printf("Address of num: %p\n", ptr); // Printing the pointer itself (address)
return 0;
}
Explanation:
- int num = 10; declares a regular integer variable num.
- int *ptr = # declares a pointer ptr that stores the address of the variable num.
- *ptr dereferences the pointer to access the value stored at that address (which is 10 in this case).
- ptr prints the address stored in the pointer.
Output:
Value of num: 10
Address of num: 0x7ffee72ab8fc // (The address may vary)
Pointer Types in C:
- Pointers to Different Data Types:
- A pointer can point to any type of variable, such as int, char, float, etc.
- The type of the pointer must match the type of the variable it points to.
Example:
C
int num = 5;
int *int_ptr = # // Pointer to an int
char ch = 'A';
char *char_ptr = &ch; // Pointer to a char
- Pointer to a Pointer (Multi-Level Pointers):
- A pointer can also point to another pointer, creating a chain of pointers.
Example:
C
int num = 10;
int *ptr = #
int **ptr2 = &ptr; // Pointer to pointer (pointer to ptr)
You can dereference multi-level pointers multiple times to access the final value.
Pointer Arithmetic:
Pointers in C can be incremented and decremented. This is especially useful for navigating arrays or buffers.
- Incrementing and Decrementing Pointers: When a pointer is incremented or decremented, it moves by the size of the type it points to. For example, if a pointer points to an int, it moves by the size of an int (usually 4 bytes on most systems).
Example:
C
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
// Accessing array elements using pointer arithmetic
printf("%d\n", *ptr); // Output: 10
ptr++; // Move to next element in array
printf("%d\n", *ptr); // Output: 20
ptr++; // Move to next element in array
printf("%d\n", *ptr); // Output: 30
- Pointer Arithmetic on Arrays: Arrays in C are closely related to pointers, and you can treat the array name as a pointer to its first element.
Null Pointers:
A null pointer is a pointer that does not point to any valid memory address. It is often used to indicate that a pointer is not initialized or is intended to be empty.
- Initializing to Null:
C
int *ptr = NULL;
- Checking for Null Pointer: It is common practice to check if a pointer is NULL before dereferencing it to avoid segmentation faults or undefined behavior.
C
if (ptr != NULL) {
// Safe to dereference the pointer
printf("%d\n", *ptr);
}
Dynamic Memory Allocation Using Pointers:
Pointers are used in C for dynamic memory allocation using functions like malloc(), calloc(), and realloc(). These functions allocate memory on the heap during runtime, and the pointer holds the address of the allocated memory.
- malloc() Example:
C
int *ptr = (int*)malloc(sizeof(int) * 5); // Allocate memory for 5 integers
if (ptr != NULL) {
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1; // Initialize the array
}
free(ptr); // Free the allocated memory
}
- calloc() Example: Similar to malloc(), but it initializes the allocated memory to zero.
C
int *ptr = (int*)calloc(5, sizeof(int)); // Allocate memory for 5 integers and initialize to zero
- realloc() Example: Changes the size of previously allocated memory.
C
ptr = (int*)realloc(ptr, sizeof(int) * 10); // Reallocate memory to hold 10 integers
Common Pointer Use Cases:
- Function Arguments: Pointers allow functions to modify the actual values of the arguments passed to them, enabling pass-by-reference behavior.
- Array and String Manipulation: Arrays are closely related to pointers in C, and pointers allow efficient traversal and modification of arrays and strings.
- Dynamic Memory Allocation: Pointers are used with malloc(), calloc(), and free() for managing memory dynamically during program execution.
- Data Structures: Pointers are critical for implementing complex data structures like linked lists, trees, and graphs.
Summary:
- Pointers hold memory addresses of variables and allow indirect access to data.
- Dereferencing (*) and Address-of (&) operators are used to interact with the values and memory addresses.
- Pointer Arithmetic allows for efficient manipulation of arrays and buffers.
- Null pointers indicate that a pointer does not point to a valid memory location.
- Pointers are essential for dynamic memory allocation and working with complex data structures in C.
