Two-Dimensional Arrays Using Arrays of Pointers to Represent
Using Arrays of Pointers to Represent Two-Dimensional Arrays
An array of pointers can be used to represent a two-dimensional array in a safe and ethical way. To do this, we can use the following guidelines:
- The array of pointers should be declared as a pointer to pointers. For example, if the two-dimensional array is of type int, then the array of pointers would be declared as int **ptr.
- The elements of the array of pointers should be initialized to NULL. This can be done using a loop.
- When accessing an element of the two-dimensional array, we should first check to make sure that the pointer is not NULL. If the pointer is NULL, then we should print an error message.
- We should always dereference the pointers before accessing the elements of the two-dimensional array. This will prevent us from accessing invalid memory.
Example of how to use an array of pointers to represent a two-dimensional array:
int **ptr;
int n, m;
// Get the number of rows and columns of the two-dimensional array.
scanf("%d %d", &n, &m);
// Allocate memory for the array of pointers.
ptr = malloc(sizeof(int *) * n);
// Initialize the array of pointers to NULL.
for (int i = 0; i < n; i++) {
ptr[i] = NULL;
}
// Allocate memory for the rows of the two-dimensional array.
for (int i = 0; i < n; i++) {
ptr[i] = malloc(sizeof(int) * m);
}
// Initialize the elements of the two-dimensional array.
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &ptr[i][j]);
}
}
// Print the elements of the two-dimensional array.
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", ptr[i][j]);
}
printf("\n");
}
// Free the memory allocated for the array of pointers and the rows of the two-dimensional array.
for (int i = 0; i < n; i++) {
free(ptr[i]);
}
free(ptr);
In this code, we first declare an array of pointers ptr.
We then declare two integer variables n
and m
to store the number of rows and columns of the two-dimensional array.
Next, we allocate memory for the array of pointers using the malloc()
function. We then initialize the array of pointers to NULL.
After that, we allocate memory for the rows of the two-dimensional array. We do this by looping through the array of pointers and calling the malloc()
function for each element.
Finally, we initialize the elements of the two-dimensional array by reading values from the user. We then print the elements of the two-dimensional array.
At the end of the program, we free the memory allocated for the array of pointers and the rows of the two-dimensional array using the
free()
function.
Further Reading:
2D Dimensional Array in c programming
Write a C Program to reverse 2D array Elements
Integer 2D array Passing to function in c
String 2D array to a Function in c
Write a C program to short 2D array using bubble sort
c program to allocate dynamic memory for 2D array integers
Using Arrays of Pointers to Represent Two-Dimensional Arrays
How to Calculate of sub element of 2D array and How to print it in c
Write a C Program to scan 5 Elements and print on the screen using 2D array
Assingement of 2D array in c
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [[email protected]].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!