In this tutorial, we are going to learn about pointers with one-dimensional and two-dimensional arrays.
Pointers with 1-D arrays:
An array variable name itself represents a pointer to the first element of that array.
In the statement int num[10];, the address of the first array element can be expressed as either &num[0] or num. Similarly, the address of the second array element can be written as &num[1] or as num +1, and so on.
In general, the address of the ith array element can be expressed as either &num[i] or (num +i).
Thus, the address of an array element can be expressed in two ways: either by writing the actual array element proceeded by the ampersand or by writing an expression in which the subscript is added to the array variable name.
The expressions &num[i] and (num + i) both represent the address of the ith element of num, and num[i] and *(num +i) both represent the value at that address.
The address of the given array element can be found by using the formula:
Address of num[i] = base address + i * scale factor // scale factor is the size of the data type used to create an array
By using heap memory, A programmer can create a new one-dimensional array at the run time and it can be accessed by using the pointers.
To create heap memory, the programmer can use either malloc() or calloc().
Let’s consider an example:
#include <stdio.h>
#include <stdlib.h>
void main() {
int *p, n, i;
printf("Enter n value : ");
scanf("%d", &n);
p = (int *) malloc(n * sizeof(int));
printf("Enter %d values : ", n);
for (i = 0; i < n; i++) {
scanf("%d", p + i);
}
printf("The given array elements are : ");
for (i = 0; i < n; i++) {
printf("%d ", *(p + i));
}
}
Output:
Enter n value : 10
Enter 10 values : 1
2
3
4
5
6
7
8
5
2
The given array elements are : 1 2 3 4 5 6 7 8 5 2
Understanding pointers with one-dimensional arrays:
Like a normal variable, a pointer variable can also be passed as an argument to the function.
A pointer variable can send the address to the formal parameter, on the other side, the formal parameter can also be declared as the same as the pointer variable (like call by address).
Let’s consider an example:
#include <stdio.h>
#include <stdlib.h>
void read(int *, int);
void display(int *, int);
void main() {
int *p, n, i;
printf("Enter n value : ");
scanf("%d", &n);
p = (int *) malloc(n*sizeof(int));
read(p, n);
printf("The given elements are : ");
display(p, n);
printf("\n");
}
void read(int *a, int n) {
int i;
printf("Enter %d values : ", n);
for (i = 0; i < n; i++) {
scanf("%d", a + i);
}
}
void display(int *a, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", *(a + i));
}
}
Output:
Enter n value : 4
Enter 4 values : 12
55
74
53
The given elements are : 12 55 74 53
Pointers with 2-D arrays:
We know that an array variable name itself represents a pointer to the first element of that array.
In the statement int num[5][5];, the address of the first row first column element can be expressed as either &num[0][0] or num. Similarly, the address of the first row second column element can be written as &num[0][1] or (num + 0 * 5 + 1), and so on.
The address of the given array element can be found by using the formula:
Address of num[i][j] = base address + i * total number of columns * scale factor + j * scale factor // scale factor is the size of the data type of the array
Using heap memory, a programmer can create a new two-dimensional array during the run-time which can be accessed using pointers.
To create heap memory, a programmer can use either malloc()
or calloc()
function.
Let’s consider an example:
#include <stdio.h>
#include <stdlib.h>
void main() {
int *p, m, n, i, j;
printf("Enter row and column size : ");
scanf("%d %d", &m, &n);
p = (int*)calloc(m*n, sizeof(int));
printf("Enter %d matrix elements : ", m*n);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", p + i * n + j);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", *(p + i * n + j));
}
printf("\n");
}
free(p);
}
Output:
Enter row and column size : 2
2
Enter 4 matrix elements : 4
5
2
3
The given matrix is
4 5
2 3
The array of Pointers:
Whenever addresses are stored as array elements, such an array is called an array of pointers.
The format for the declaration of an array of pointers is
data_type *array_name [size];
(or)
data_type *(array_name [size]);
The following declaration creates an array named ptr_array containing 5 elements, each of which is of type int *.
int *ptr_array[5];
The above declaration creates an array of pointers with size 5, named as ptr_array. In each location, the user needs to store the addresses of another variable of the same data type int.
Let’s consider an example:
#include <stdio.h>
#include <stdlib.h>
void main() {
float *a[5], f1, f2;
f1 = 10.17;
f2 = 35.10;
a[0] = &f1;
a[1] = &f2;
printf("First Value = %f\n", *(a[0]);
printf("Second Value = %f\n", *(a[1]);
}
Output:
First Value = 10.170000
Second Value = 35.099998
The 2-D array as an array of pointers:
A multidimensional array can be expressed in terms of an array of pointers rather than as a pointer to a group of contiguous arrays.
In such cases, the newly defined array will have one less dimension than the original array. Each pointer will indicate the beginning of a separate (n – 1)th dimensional array.
In general, a two-dimensional array can be defined as a one-dimensional array of pointers as Data_type *array_name [size1]; rather than Data_type array_name[size1][size2];.
Similarly, an n-dimensional array can be defined as an (n – 1)th dimensional array of pointers as Data_type *array_name[size1] [size2] ———- [sizen-1]; instead of Data_type array_name[size1] [size2][size3]———–[sizen];.
Here the preceding asterisk (*) establishes that the array will contain pointers.
Consider the following example:
int *num[10];
Here num[0] points to the beginning of the first row, num[1] points to the beginning of the second row, and so on. In this kind of definition, the number of elements in each row is specified at the run time.
An individual array element, such as array[2][4] can be accessed by *(array[2] + 4). In this expression, array[2] is a pointer to the first element in row 2, so that (ary[2] + 4) points to element 4 (actually the fifth element).
The heap memory is allocated individually for each pointer of the array in an array of pointers.
#include <stdio.h>
#include <stdlib.h>
void main() {
int *p[10], n, m, i, j;
printf("Enter row and column sizes : ");
scanf("%d %d", &m, &n);
for (i = 0; i < m; i++) {
p[i] = (int *) malloc(n * sizeof(int));
}
printf("Enter %d elements : ", n*m);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", p[i] + j);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", *(p[i] + j));
}
printf("\n");
}
}
Output:
Enter row and column sizes : 2
2
Enter 4 elements : 1
2
3
5
The given matrix is
1 2
3 5
REFERENCES:
Happy Learning 🙂