MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Arrays and Pointers

In C, the name of an array is a constant pointer to the first element of the array. This means that arr (an array) and &arr[0] (the address of the first element) are equivalent.

Example

#include <stdio.h>
int main() { int arr[5] = {1, 2, 3, 4, 5}; // Declare and initialize an array // Print array elements using array indexing for (int i = 0; i < 5; i++) { printf("arr[%d] = %d\n", i, arr[i]); } // Print array elements using pointers for (int i = 0; i < 5; i++) { printf("*(arr + %d) = %d\n", i, *(arr + i)); } return 0; }

here, arr[i]: Accesses the ith element of the array, *(arr + i): Uses the array name (arr) as a pointer and adds i to get the address of the ith element. Dereferencing (*) this address gives the value of the ith element.


Pointer to an Array

You can also declare a pointer to an array and use it to access array elements.

Example

#include <stdio.h>
int main() { int arr[5] = {1, 2, 3, 4, 5}; // Declare and initialize an array int *ptr = arr; // Pointer to the first element of the array // Print array elements using the pointer for (int i = 0; i < 5; i++) { printf("*(ptr + %d) = %d\n", i, *(ptr + i)); } return 0; }

Array as Function Arguments

When passing an array to a function, what is actually passed is a pointer to the first element of the array.

Example

#include <stdio.h>
// Function to print array elements void printArray(int *arr, int size) { for (int i = 0; i < size; i++) { printf("arr[%d] = %d\n", i, arr[i]); } } int main() { int arr[5] = {1, 2, 3, 4, 5}; // Declare and initialize an array int size = sizeof(arr) / sizeof(arr[0]); printArray(arr, size); // Pass the array to the function return 0; }



Multidimensional Arrays

A multidimensional array is an array of arrays. The most common form is a two-dimensional array, which can be thought of as a matrix or a table with rows and columns.

Example of a Two-Dimensional Array

#include <stdio.h>
int main() { int matrix[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Accessing elements printf("Element at [1][2]: %d\n", matrix[1][2]); // Outputs 7 return 0; }


Pointers and Multidimensional Arrays

Array of Arrays

In the case of a two-dimensional array, each element of the main array is a sub-array. The name of the array acts as a pointer to the first sub-array.

int matrix[3][4]; // A two-dimensional array with 3 rows and 4 columns

Here, matrix can be considered as a pointer to an array of 4 integers.

Pointer to an Array

You can declare a pointer to an array and use it to access elements in a multidimensional array.

int (*ptr)[4]; // Pointer to an array of 4 integers
ptr = matrix; // Points to the first row of the matrix

Accessing Elements Using Pointers

You can access elements of a multidimensional array using pointers with pointer arithmetic.

Example

#include <stdio.h>
int main() { int matrix[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; int (*ptr)[4] = matrix; // Pointer to an array of 4 integers // Accessing elements using pointer arithmetic printf("Element at [1][2]: %d\n", *(*(ptr + 1) + 2)); // Outputs 7 // Accessing elements using array notation with pointers printf("Element at [2][3]: %d\n", ptr[2][3]); // Outputs 12 return 0; }

Understanding how pointers interact with multidimensional arrays allows for efficient data manipulation and is fundamental for advanced programming in C.

Report an issue

Reporting: Arrays and Pointers (topic)

Related Posts