POINTERS AND ARRAYS:
What is an array?
An array is a data structure consisting of elements of similar data type. The elements of an array are stored in consecutive memory locations.
How to declare an array?
Below is the template for array declaration.
datatype name[number of elements];
int num[10];
Here, num is array of 10 integers.
How to access array elements?
Array indices are used to access array elements. And the array index starts from zero.
int arr[5] = {1, 2, 3, 4, 5};
arr[0] gives us the element at the 0th index in the array arr. Let us try to read the array elements one by one.
arr[0] is 1
arr[1] is 2
arr[2] is 3
arr[3] is 4
arr[4] is 5
What is an array?
An array is a data structure consisting of elements of similar data type. The elements of an array are stored in consecutive memory locations.
How to declare an array?
Below is the template for array declaration.
datatype name[number of elements];
int num[10];
Here, num is array of 10 integers.
How to access array elements?
Array indices are used to access array elements. And the array index starts from zero.
int arr[5] = {1, 2, 3, 4, 5};
arr[0] gives us the element at the 0th index in the array arr. Let us try to read the array elements one by one.
arr[0] is 1
arr[1] is 2
arr[2] is 3
arr[3] is 4
arr[4] is 5
How to access array elements using pointers?
To access array elements, assign the base address of array to the pointer.
int *ptr;
int arr[5] = {10, 20, 30, 40, 50};
ptr = &arr[0];
Now, the pointer refers to the address of first element in the array arr.
*(ptr + 0) refers to the content of arr[0]
Then, *(ptr + i) refers to the content of arr[i]. So, the element at the array index i can be access by dereferencing the pointer (ptr + i).
ie. *(ptr + i) <=> arr[i]
How to write values in an array using pointers?
To write values in an array using pointers, assign the base address of array to pointer. Then, assing value to the object refereced by pointer as shown below.
*(ptr + 0) = 100;
*(ptr + 1) = 200;
*(ptr + 2) = 300;
*(ptr + 3) = 400;
*(ptr + 4) = 500;
So, we have updated the array elements using pointers and the element in the array arr are as follows.
arr[5] = {100, 200, 300, 400, 500};
Below program explains how to read values from an array and write values to array using pointers.
#include <stdio.h>
int main() {
int i, arr[5], input;
printf("Enter array inputs:\n");
for (i = 0; i < 5; i++) {
printf("arr[%d]: ", i);
scanf("%d", &input);
/* assigning values to array elements using pointers */
*(arr + i) = input;
}
printf("\nReading array elements using pointer:\n");
for (i = 0; i < 5; i++) {
printf("arr[%d]: %d\n", i, *(arr + i));
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter array inputs:
arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50
Reading array elements using pointer:
arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50
Enter array inputs:
arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50
Reading array elements using pointer:
arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50
How to access two dimensional array using pointers?
Consider the following two dimensional array.
int num[2][2] = {{1, 2}, {3, 4}};
Pointer notation Subscript notation Value
**num num[0][0] 1
*(*num + 1) num[0][1] 2
*(*(num + 1)) num[1][0] 3
*(*(num + 1) + 1) num[1][1] 4
Using the above pointer notation, we can access two dimensional array using pointers.
How to write values into a two dimensional array using pointers?
**num = 10;
*(*num + 1) = 20;
*(*(num + 1)) = 30;
*(*(num + 1) + 1) = 40;
So, we have updated the values of a two dimensional array. And the elements in the array num are as follows.
num[2][2] = {{10, 20}, {30, 40}};
Below program explains how to read values from a multidimensional array and write values to a multidimensional array.
#include <stdio.h>
int main() {
int i, j, num[2][2] = {{1, 2}, {3, 4}};
printf("Reading two dimensional array using pointer:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("num[%d][%d]: %d\n", i, j, *(*(num + i) + j));
}
}
/* writing values to a 2d array */
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
*(*(num + i) + j) = num[i][j] + 100;
}
}
printf("\nReading the updated values in 2d array using pointer:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("num[%d][%d]: %d\n", i, j, *(*(num + i) + j));
}
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Reading two dimensional array using pointer:
num[0][0]: 1
num[0][1]: 2
num[1][0]: 3
num[1][1]: 4
Reading the updated values in 2d array using pointer:
num[0][0]: 101
num[0][1]: 102
num[1][0]: 103
num[1][1]: 104
Reading two dimensional array using pointer:
num[0][0]: 1
num[0][1]: 2
num[1][0]: 3
num[1][1]: 4
Reading the updated values in 2d array using pointer:
num[0][0]: 101
num[0][1]: 102
num[1][0]: 103
num[1][1]: 104
Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324
ReplyDelete