C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we will learn about array pointers in the C programming language. Array pointers, also known as pointers to arrays, are pointers that point to the first element of an array. They are useful when working with functions that require passing arrays or for dynamic memory allocation.
To declare a pointer to an array, specify the data type, followed by an asterisk (*) for the pointer, the name of the pointer, and the size of the array enclosed in square brackets.
int (*arrayPointer)[5]; // Declare a pointer to an array of 5 integers
In this example, arrayPointer
is a pointer to an array of 5 integers. It can be used to point to any array with 5 integer elements.
To assign the address of an array to a pointer, use the name of the array (which is a pointer to the first element).
int myArray[5] = {1, 2, 3, 4, 5}; arrayPointer = &myArray;
To access elements in an array using an array pointer, use the pointer name followed by the index enclosed in square brackets.
int firstElement = (*arrayPointer)[0]; // Access the first element of the array int thirdElement = (*arrayPointer)[2]; // Access the third element of the array
Here's an example demonstrating how to iterate over an array using an array pointer:
#include <stdio.h> int main() { int myArray[5] = {1, 2, 3, 4, 5}; int (*arrayPointer)[5] = &myArray; // Iterate over the array using an array pointer for (int i = 0; i < 5; i++) { printf("%d ", (*arrayPointer)[i]); } return 0; }
The output will be:
1 2 3 4 5
That's it for our tutorial on array pointers in the C programming language. Understanding how to use pointers with arrays is essential when working with dynamic memory allocation, passing arrays to functions, or when passing multidimensional arrays as function arguments.
Pointer to array in C example:
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptrToNumbers = numbers; // Accessing elements using pointer printf("Element at index 2: %d\n", *(ptrToNumbers + 2)); return 0; }
numbers
and a pointer ptrToNumbers
pointing to its first element.How to declare and use array pointers in C:
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptrToNumbers = numbers; // Using array pointer printf("First element: %d\n", *ptrToNumbers); return 0; }
Passing array pointers to functions in C:
#include <stdio.h> // Function taking an array pointer void printArray(int *arr, int size) { for (int i = 0; i < size; ++i) { printf("%d ", arr[i]); } printf("\n"); } int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptrToNumbers = numbers; // Passing array pointer to a function printArray(ptrToNumbers, 5); return 0; }
Dynamic allocation of arrays using pointers in C:
#include <stdio.h> #include <stdlib.h> int main() { int size = 5; int *dynamicArray = (int *)malloc(size * sizeof(int)); // Remember to free memory when done free(dynamicArray); return 0; }