1.4 Pointers and 2-D Arrays | Two dimensional Array | Data
Structures & Algorithm Tutorials
Jenny's Lectures CS IT
2D Arrays and Pointers
In this lesson, we will discuss how 2D arrays are related to
pointers and how you can access the elements of a 2D array
with the help of pointers. Let us take the example of a 3x3
matrix:
659
123
487
This 2D array can be initialized at compile time and is an array
of arrays. Each 1D array has three integer elements and the 2D
array is an array of 3 1D arrays. The element at [0][0] is 6, and
the base address of the array is 100.
A pointer variable can be declared to contain the address of an
integer variable. However, the name of the 2D array cannot be
used as it returns a pointer to the first element, which is a 1D
array and not an integer variable. Instead, the address of [0][0]
can be used.
To print the address of the 2D array, you can use the format
specifier %p for hexadecimal or %u for unsigned integer. You
can also use the name of the array or the address of [0][0].
The value at the address can be printed using the
dereferencing operator (*).
When accessing a 1D array, the name of the array will return
the address of the first element, while adding 1 to the pointer
will point to the next element. Similarly, when accessing a 2D
array, the name of the array will return a pointer to the first
element, and adding 1 to the pointer will point to the next
element. To print the value at a specific index of the array, use
the dereferencing operator. You can also use a double pointer
to access values in a 2D array. To access a value at a specific
index, use the expression a[i][j] or s*(a+i)+j. You can also use
a pointer variable instead of the array name to access values.
Structures & Algorithm Tutorials
Jenny's Lectures CS IT
2D Arrays and Pointers
In this lesson, we will discuss how 2D arrays are related to
pointers and how you can access the elements of a 2D array
with the help of pointers. Let us take the example of a 3x3
matrix:
659
123
487
This 2D array can be initialized at compile time and is an array
of arrays. Each 1D array has three integer elements and the 2D
array is an array of 3 1D arrays. The element at [0][0] is 6, and
the base address of the array is 100.
A pointer variable can be declared to contain the address of an
integer variable. However, the name of the 2D array cannot be
used as it returns a pointer to the first element, which is a 1D
array and not an integer variable. Instead, the address of [0][0]
can be used.
To print the address of the 2D array, you can use the format
specifier %p for hexadecimal or %u for unsigned integer. You
can also use the name of the array or the address of [0][0].
The value at the address can be printed using the
dereferencing operator (*).
When accessing a 1D array, the name of the array will return
the address of the first element, while adding 1 to the pointer
will point to the next element. Similarly, when accessing a 2D
array, the name of the array will return a pointer to the first
element, and adding 1 to the pointer will point to the next
element. To print the value at a specific index of the array, use
the dereferencing operator. You can also use a double pointer
to access values in a 2D array. To access a value at a specific
index, use the expression a[i][j] or s*(a+i)+j. You can also use
a pointer variable instead of the array name to access values.