0% found this document useful (0 votes)
5 views3 pages

Array Manipulation with Pointers in C

This document provides an overview of array manipulation using pointers in C, highlighting how pointers can efficiently access, modify, and traverse array elements. It includes examples of accessing elements, modifying values, reading inputs, and displaying memory addresses, emphasizing the importance of pointer arithmetic. Key points include the efficiency of pointer usage and the necessity of maintaining pointer safety to avoid errors.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Array Manipulation with Pointers in C

This document provides an overview of array manipulation using pointers in C, highlighting how pointers can efficiently access, modify, and traverse array elements. It includes examples of accessing elements, modifying values, reading inputs, and displaying memory addresses, emphasizing the importance of pointer arithmetic. Key points include the efficiency of pointer usage and the necessity of maintaining pointer safety to avoid errors.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Notes: Array Manipulation Using Pointers

For First Year Mechanical Engineering Students

1. Introduction
Pointers provide a powerful way to access and manipulate array elements in C.
Using pointers, you can read, modify, and traverse arrays efficiently, often with less and
faster code than using array subscripts alone [1] [2] [3] .

2. Accessing Array Elements Using Pointers


The name of an array acts as a pointer to its first element.
Elements can be accessed using pointer arithmetic:
arr[i] is equivalent to *(arr + i) [1] [4] .

Example:
int arr[^4] = {10, 20, 30, 40};
printf("%d\n", *(arr + 2)); // Prints 30 (3rd element)

You can also use a pointer variable to traverse the array:


int arr[^5] = {5, 15, 25, 35, 45};
int *ptr = arr;
for(int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
// Output: 5 15 25 35 45

Or by incrementing the pointer itself:


int *ptr = arr;
for(int i = 0; i < 5; i++) {
printf("%d ", *ptr);
ptr++; // Move pointer to next element
}
// Output: 5 15 25 35 45

Accessing elements with *(arr + i) or *ptr (after increment) both work [1] [2] [5] .
3. Modifying Array Elements with Pointers
You can change an element's value using pointers:
int numbers[^4] = {25, 50, 75, 100};
*numbers = 13; // Change first element to 13
*(numbers + 1) = 17; // Change second element to 17

Looping with a pointer for bulk modifications:


int nums[^5] = {1, 2, 3, 4, 5};
int *ptr = nums;
for(int i = 0; i < 5; i++) {
*ptr *= 2; // Double each element
ptr++;
}
// Now nums = {2, 4, 6, 8, 10}

4. Reading and Processing Input Arrays Using Pointers


You can use pointers to read inputs and immediately process or analyze them:
int arr[^6], sum = 0;
for(int i = 0; i < 6; ++i) {
scanf("%d", arr + i); // Equivalent to scanf("%d", &arr[i]);
sum += *(arr + i); // Add each input to sum
}
printf("Sum = %d", sum);

Using arr + i passes the correct address for input and manipulation [3] .

5. Displaying Memory Addresses with Pointers


Pointers can show actual memory addresses, illustrating how arrays are stored contiguously:
int arr[^4] = {10, 20, 30, 40};
for(int i = 0; i < 4; i++) {
printf("Address of arr[%d]: %p\n", i, (arr + i));
}

On most systems, you’ll see addresses increase by 4 bytes for integers, confirming
contiguous allocation [1] .

6. Key Points and Best Practices


Efficiency: Pointer traversal is often faster, especially for large arrays [1] .
Pointer increments move by the size of the data type (e.g., 4 bytes for int).
Safety: Always ensure pointers stay within valid array bounds to avoid undefined
behavior [1] [3] .
Manipulation Using Array Subscript Using Pointer Arithmetic

Access element arr[i] *(arr + i)

Modify element arr[i] = 42; *(arr + i) = 42;

Input element scanf("%d", &arr[i]); scanf("%d", arr + i);

7. Summary
Pointers allow efficient access, traversal, and modification of arrays, making them vital for
low-level and high-performance programming [1] [2] [3] [5] .
Pointer arithmetic (ptr + i, ptr++) gives direct control over array manipulation.
Always use care: pointer mistakes can lead to bugs or memory errors.

1. [Link]
2. [Link]
3. [Link]
4. [Link]
5. [Link]

You might also like