- 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 to an Array
![]() Share with a Friend |
C Programming - C Pointer to an Array
C Pointer to an Array
In C, you can create a pointer that points to the first element of an array. Understanding pointers to arrays is important because it enables efficient manipulation of array elements, passing arrays to functions, and performing pointer arithmetic.
An array name in C is essentially a pointer to the first element of the array, so understanding how pointers and arrays work together is crucial for efficient programming.
Declaration of Pointer to an Array
To declare a pointer that points to an array, you would declare it as:
C
type (*pointer_name)[array_size];
- type: The data type of the array elements.
- pointer_name: The name of the pointer variable.
- array_size: The size of the array.
This syntax means that pointer_name will point to an array of array_size elements of type type.
Example 1: Pointer to an Array
C
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Declare a pointer to the array
int (*ptr)[5];
// Point to the entire array 'arr'
ptr = &arr;
// Accessing array elements through the pointer
printf("Element 0: %d\n", (*ptr)[0]);
printf("Element 1: %d\n", (*ptr)[1]);
printf("Element 2: %d\n", (*ptr)[2]);
printf("Element 3: %d\n", (*ptr)[3]);
printf("Element 4: %d\n", (*ptr)[4]);
return 0;
}
Output:
Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
In this example:
- ptr is declared as a pointer to an array of 5 integers (int (*ptr)[5]).
- We assign ptr to the address of the array arr.
- To access elements of the array, we dereference the pointer and use the array index, like (*ptr)[index].
Example 2: Pointer to an Array as a Function Argument
When passing an array to a function, you can use a pointer to an array. This allows the function to manipulate the array directly.
C
#include <stdio.h>
// Function that takes a pointer to an array of 5 integers
void printArray(int (*arr)[5]) {
for (int i = 0; i < 5; i++) {
printf("%d ", (*arr)[i]);
}
printf("\n");
}
int main() {
int arr[5] = {10, 20, 30, 40, 50};
// Passing pointer to the array
printArray(&arr);
return 0;
}
Output:
10 20 30 40 50
In this example:
- The function printArray accepts a pointer to an array of 5 integers (int (*arr)[5]).
- We pass the address of the array arr (&arr) to the function.
- Inside the function, the array elements are accessed through the pointer by dereferencing it ((*arr)[i]).
Example 3: Pointer Arithmetic with Array
Since an array name is essentially a pointer to its first element, pointer arithmetic can be used to iterate through the array.
C
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // Pointer to the first element of the array
// Using pointer arithmetic to access elements
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(ptr + i)); // Accessing using pointer arithmetic
}
return 0;
}
Output:
Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
In this example:
- The pointer ptr is assigned the address of the first element of the array arr.
- We then use pointer arithmetic (*(ptr + i)) to access each element of the array.
- The ptr + i gives the address of the ith element, and * dereferences it to get the value.
Key Points:
- Array Name as a Pointer: In C, the name of an array is a constant pointer to the first element, so arr can be used as a pointer.
- Pointer to an Array: To declare a pointer to an entire array, the syntax is type (*ptr)[size], where ptr points to an array of size elements of type type.
- Pointer Arithmetic: You can use pointer arithmetic to navigate through the elements of an array. Adding an integer to a pointer moves it by that many elements, not bytes.
Conclusion:
A pointer to an array is useful when you need to work with the entire array in a function or when dealing with large data structures. Understanding how pointers and arrays work together in C helps in writing efficient code, especially when working with multi-dimensional arrays, dynamic memory allocation, or performing pointer arithmetic.
