Lecture 3b - Pointer and Arrays Spring 2025
Lecture 3b - Pointer and Arrays Spring 2025
int List[5 ] = { 9, 7, 5, 3, 1 };
std::cout << "Element 0 is at address: " << &List[0] << '\n’; //0x7ffffcc20
std::cout << "Element 1 is at address: " << &List[1] << '\n’; //0x7ffffcc24
std::cout << "Element 2 is at address: " << &List[2] << '\n’; //0x7ffffcc28
std::cout << "Element 3 is at address: " << &List[3] << '\n’; //0x7ffffcc2c
std::cout << "Element 4 is at address: " << &List[4] << '\n’; //0x7ffffcc30
▪ Each of these memory addresses is 4 bytes apart, which is the size of an integer
Relationship Between Pointers and Arrays (Cont.)
• Accessing array elements with pointers
• Assume declarations:
int List[ 5 ]; //declare array
int *LPtr; //declare a pointer
LPtr = List; //Method 1: Address of first Element of array List
LPtr = &List[0]; //Method 2: Address of first Element of array List
Effect:-
- Name of the array is also the address of its first element
- List is an address, no need for & operator
- The LPtr pointer will contain the address of the first element of the array List.
• Element List[ 2 ] can also be accessed using LPtr[2].
Accessing 1-Demensional Array Using Pointers
▪ We know, Array name denotes the memory address of its
first slot. Address Data
▪ Example: 0x7ffcc980 Element 0
… 0x7ffcc982 Element 1
… 0x7ffcc984 Element 2
7
Pointer to an Array
Initially the value held by p and ptr is same, however,
arithmetic on both pointers give a different result as
both of these pointers are of different types.
8
Pointer to an Array
9
Class activity
• Say Array[5] is an int array:
int Array[5] = {5,7,4,8,9};
11
Memory layout of 2D array
• In C/C++, a multidimensional array is just an array of arrays.
• C/C++ stores arrays declared as two-dimensional using a one-
dimensional array
• The first elements stored are those in the first row (in order). Then, the second
row is stored, etc.
• This memory allocation policy is called “row-major ordering”.
• For example, when we declare the following 2d array
int D[4][3]
• Note: type (arr + i) and *(arr+i) points to same address but their base types
are completely different.
• base type of (arr + i) is a pointer to an array of 4 integers (in our example)
• From previous slides: *(arr + i) points to the base address of every ith 1-D
array and it is of base type pointer to int
• Now let’s use pointer arithmetic
• *(arr + i)+0 points to the address of the 0th element of the 1-D array.
• *(arr + i) + 1 points to the address of the 1st element of the 1-D array
• *(arr + i) + 2 points to the address of the 2nd element of the 1-D array
• Generally, *(arr + i) + j points to the base address of jth element of ith 1-D
array.
• After finding the base address, we can find the value by dereferencing the
pointer to the base address, i.e., *( *(arr + i) + j)
Program: Indexing through 2D array
#include<iostream>
using namespace std;
int main()
{
int arr[3][4] = { {11,22,33,44}, {55,66,77,88}, {11,66,77,44} };
int i, j;
• If aiData is the 2D array, then aiData is the address of the first element of the
2d array i.e., first row.
aiData[0] aiData[1] aiData[2]
• And dereferencing aiData, i.e., *aiData or *(aiData+0), gives the address of the
first element of the first row. [See next slide for visual description]
Example: Array names and pointers.
• aiData+0 is the address of the first element of the 2d array i.e., first row.
• aiData+1 is the address of the second element of the 2d array i.e.,
second row.
• aiData+2 is the address of the third element of the 2d array i.e., third
row.
Example: Dereferencing the pointers.
• Dereferencing aiData+1 gives the address of the first element of the 1th
row,
• To access the jth element of the ith 1-D array, we use *(p+i)+j
return 0;
}
Class activity
• You have the following 2D array, Array[3][7].
int NUMROWS = 3;
int NUMCOLS = 7;
int Array[NUMROWS][NUMCOLS];
0 1 2 3 4 5 6
0 4 18 9 3 4 6 0
1 12 45 74 15 0 98 0
2 84 87 75 67 81 85 79
int NUMROWS = 3;
int NUMCOLS = 7;
int Array[NUMROWS][NUMCOLS];
0 1 2 3 4 5 6
0 4 18 9 3 4 6 0
1 12 45 74 15 0 98 0
2 84 87 75 67 81 85 79
int NUMROWS = 3;
int NUMCOLS = 7;
int Array[NUMROWS][NUMCOLS];
0 1 2 3 4 5 6
0 4 18 9 3 4 6 0
1 12 45 74 15 0 98 0
2 84 87 75 67 81 85 79