- 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 Pointer vs Array
![]() Share with a Friend |
C Programming - C Pointer vs Array
C Pointer vs Array
In C programming, pointers and arrays are closely related, but they are not the same. Understanding their differences and similarities is crucial for efficient memory management and programming. Below, we will compare pointers and arrays in C, covering their characteristics, differences, and use cases.
- Definition:
- Array: An array is a collection of elements of the same type, stored in contiguous memory locations. The array name represents a pointer to the first element of the array.
C
int arr[5] = {1, 2, 3, 4, 5};
- Pointer: A pointer is a variable that holds the address of another variable or data. Pointers can point to any data type, including arrays, structures, or other pointers.
C
int *ptr;
- Memory Allocation:
- Array: Arrays are statically allocated when declared. This means the memory for an array is allocated at compile time.
- The size of the array is fixed and cannot be changed during runtime.
- Pointer: Pointers are dynamically allocated using functions like malloc(), calloc(), or realloc() during runtime. This provides flexibility in memory allocation.
C
int *ptr = (int *)malloc(5 * sizeof(int)); // Dynamically allocated memory for 5 integers.
- Size:
- Array: The size of an array is fixed and determined at compile time. You can use the sizeof() operator to determine the total size of the array, but you cannot resize it after declaration.
C
int arr[10];
printf("Size of array: %zu\n", sizeof(arr)); // Prints the total size of the array.
- Pointer: The size of a pointer variable is always fixed (the size of the pointer itself), but it can point to any dynamically allocated memory, which can be resized.
C
int *ptr = (int *)malloc(5 * sizeof(int));
printf("Size of pointer: %zu\n", sizeof(ptr)); // Prints the size of the pointer itself, not the allocated memory.
- Accessing Elements:
- Array: Arrays are accessed using the array index, and the array name represents the address of the first element.
C
printf("First element: %d\n", arr[0]); // Access using index
- Pointer: Pointers can be dereferenced using the dereference operator * and the pointer arithmetic.
C
printf("First element: %d\n", *ptr); // Dereferencing the pointer
- Pointer Arithmetic:
- Array: You can use the array name to represent a pointer to the first element. Array elements can be accessed using the array index, but you can also use pointer arithmetic to traverse the array.
C
int *ptr = arr;
printf("First element: %d\n", *ptr); // Equivalent to arr[0]
printf("Second element: %d\n", *(ptr + 1)); // Equivalent to arr[1]
- Pointer: Pointers allow you to perform arithmetic (e.g., increment or decrement) to navigate through memory addresses.
C
ptr = arr; // Pointer points to the first element of the array
printf("First element: %d\n", *ptr); // dereference the pointer
ptr++; // Increment pointer to the next element
printf("Second element: %d\n", *ptr);
- Array vs Pointer Representation:
- Array: The name of an array represents the base address (pointer to the first element), but it is not a pointer itself. The name of the array cannot be reassigned.
C
int arr[5];
arr = ptr; // Error: Cannot assign a new address to an array.
- Pointer: A pointer holds the address of a variable and can be reassigned to point to a different memory location.
C
int *ptr = arr;
ptr = another_ptr; // Valid: Pointers can be reassigned.
- Array vs Pointer in Function:
- Array: When an array is passed to a function, it is passed as a pointer (the address of the first element). So, modifying an array within a function affects the original array.
C
void modifyArray(int arr[]) {
arr[0] = 10; // Modifies the original array
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
modifyArray(arr);
printf("First element: %d\n", arr[0]); // Output will be 10
return 0;
}
- Pointer: You can also pass pointers to functions. The pointer passed can be modified to point to different addresses.
C
void modifyPointer(int *ptr) {
*ptr = 10; // Modifies the value pointed to by the pointer
}
int main() {
int num = 5;
modifyPointer(&num);
printf("Value: %d\n", num); // Output will be 10
return 0;
}
- Dynamic Memory Allocation:
- Array: Arrays cannot be resized during runtime. If you want to resize an array, you would have to declare a new array with a larger size and copy the elements.
- Pointer: Pointers allow dynamic memory allocation with functions like malloc(), calloc(), and realloc(). You can allocate and resize memory during runtime, which gives flexibility.
C
int *ptr = (int *)malloc(5 * sizeof(int)); // Allocate memory
ptr = (int *)realloc(ptr, 10 * sizeof(int)); // Resize the allocated memory
- Initialization:
- Array: Arrays can be initialized using a list of values, and each element is assigned its value directly.
C
int arr[5] = {1, 2, 3, 4, 5};
- Pointer: Pointers need to be initialized with an address. If you're using dynamic memory, you need to allocate memory explicitly.
C
int *ptr = (int *)malloc(5 * sizeof(int));
- Performance:
- Array: Arrays are generally faster in terms of memory access, as they are contiguous in memory and have a fixed size.
- Pointer: Pointer operations can be slower compared to array access due to the overhead of dynamic memory management. However, pointers are more flexible and efficient when managing large or dynamic data structures like linked lists.
Summary of Differences:
| Feature | Array | Pointer |
|---|---|---|
| Memory Allocation |
Statically allocated |
Dynamically allocated |
| Size |
Fixed, cannot be resized |
Variable, can point to dynamic memory |
| Accessing Elements |
Using index arr[i] |
Using pointer arithmetic *(ptr + i) |
| Pointer Arithmetic |
Array name can be treated as a pointer |
Pointer arithmetic can be performed |
| Memory |
Contiguous block |
Can point to any part of memory |
| Function Passing |
Passed as a pointer to the first element |
Passed as a pointer to the memory |
| Reassignability |
Cannot be reassigned |
Can be reassigned to point to different memory locations |
Conclusion:
Both pointers and arrays are important concepts in C programming, and while they share some similarities, such as both representing memory addresses, they have distinct differences. Arrays are best suited for fixed-size collections, while pointers provide more flexibility for dynamic memory management and complex data structures. Understanding the relationship between arrays and pointers is key to mastering C programming.
