- 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 and Arrays
![]() Share with a Friend |
C Programming - C Pointers and Arrays
C Pointers and Arrays
In C, arrays and pointers are closely related. An array is a collection of elements of the same data type, while a pointer is a variable that stores the memory address of another variable. The relationship between pointers and arrays is fundamental to understanding how memory is managed in C.
Key Concepts:
- Array as a Pointer:
- In most expressions, the name of an array represents the address of the first element of the array.
- This makes arrays and pointers closely related. In fact, when you pass an array to a function, you are passing a pointer to the first element of the array, not the entire array.
- Pointer Arithmetic with Arrays:
- Pointers can be used to navigate through array elements by using pointer arithmetic.
- You can increment or decrement pointers to access different elements in an array.
Array Declaration and Pointer Representation:
Array Declaration:
C
int arr[5] = {1, 2, 3, 4, 5};
This defines an array arr of 5 integers.
Pointer Representation:
C
int *ptr = arr; // ptr points to the first element of arr
Here, arr is treated as a pointer to the first element of the array.
Pointer and Array Relationship:
- Array Name as a Pointer:
- arr is essentially a pointer to the first element of the array arr[0].
- arr and &arr[0] both give the same memory address.
- arr[i] can be accessed using the pointer as *(arr + i).
- Pointer Arithmetic:
- In C, pointer arithmetic allows you to traverse an array using a pointer.
- Incrementing a pointer moves it to the next element in the array (i.e., it adds the size of the data type the pointer is pointing to).
C
int *ptr = arr; // Pointer to the first element of the array
printf("%d\n", *ptr); // Prints arr[0] (1)
ptr++; // Move pointer to the next element
printf("%d\n", *ptr); // Prints arr[1] (2)
- Accessing Array Elements Using Pointers:
- arr[i] can be written as *(arr + i), where arr is the address of the first element.
- Similarly, *(ptr + i) can be used to access array elements when a pointer is used.
Example of Pointer and Array Relationship:
C
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of arr
// Accessing elements using array index
printf("Array using array notation:\n");
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
// Accessing elements using pointer arithmetic
printf("\nArray using pointer arithmetic:\n");
for (int i = 0; i < 5; i++) {
printf("*(ptr + %d) = %d\n", i, *(ptr + i));
}
return 0;
}
Output:
Array using array notation:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
Array using pointer arithmetic:
*(ptr + 0) = 10
*(ptr + 1) = 20
*(ptr + 2) = 30
*(ptr + 3) = 40
*(ptr + 4) = 50
Explanation:
- arr[i] is equivalent to *(arr + i). Both expressions access the i-th element of the array.
- The pointer ptr is incremented using pointer arithmetic to access each element in the array.
Passing Arrays to Functions Using Pointers:
When you pass an array to a function, you are actually passing the address of the first element of the array, not the entire array. This is done using pointers.
Example:
C
#include <stdio.h>
void printArray(int *ptr, int size) {
for (int i = 0; i < size; i++) {
printf("Element %d: %d\n", i, *(ptr + i)); // Pointer arithmetic
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
printArray(arr, 5); // Passing array to function using pointer
return 0;
}
Output:
Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
Explanation:
- printArray takes a pointer ptr as an argument and uses pointer arithmetic to print the array elements.
Returning Arrays from Functions Using Pointers:
In C, you cannot return an array directly from a function, but you can return a pointer to an array or dynamically allocate memory for an array and return that pointer.
Example (Returning a Pointer to Array):
C
#include <stdio.h>
int* createArray(int size) {
static int arr[10]; // Static array
for (int i = 0; i < size; i++) {
arr[i] = i * 10;
}
return arr; // Returning pointer to array
}
int main() {
int size = 5;
int *ptr = createArray(size);
// Printing returned array
for (int i = 0; i < size; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
Output:
0 10 20 30 40
Explanation:
- The createArray function returns a pointer to a static array. The function fills the array and returns the pointer to the first element.
- The array is declared as static so that it persists beyond the function's scope.
Pointer to Array:
In C, you can have a pointer to an entire array, which is different from a pointer to a single element.
Example (Pointer to Array):
C
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int (*ptr)[3] = &arr; // Pointer to an array of 3 integers
// Accessing elements using pointer
printf("arr[0] = %d\n", (*ptr)[0]);
printf("arr[1] = %d\n", (*ptr)[1]);
printf("arr[2] = %d\n", (*ptr)[2]);
return 0;
}
Output:
arr[0] = 10
arr[1] = 20
arr[2] = 30
Explanation:
- (*ptr)[i] accesses the i-th element of the array arr through the pointer ptr.
Summary:
- Arrays and pointers are closely related in C. The array name acts as a pointer to the first element of the array.
- Pointer arithmetic allows easy traversal of array elements.
- Passing arrays to functions is done via pointers.
- Returning arrays from functions can be done by returning a pointer to an array or dynamically allocating memory.
- You can declare pointers to arrays to manipulate entire arrays efficiently.
Understanding the relationship between arrays and pointers is essential for working with arrays, memory, and efficient C programming.
