Examples of Pointers and One-Dimensional Array

For Complete YouTube Video: Click Here

In this class, we will understand Examples of Pointers and One-Dimensional array.

We have already discussed the concepts of arrays and pointers.

Examples of Pointers and One-Dimensional array

Example 1

The image below is example 1 on pointers and a one-dimensional array.

Examples of Pointers and One-Dimensional Array 1
Examples of Pointers and One-Dimensional Array 1

In the above example, an array of 5 elements has also been declared, as shown below.

Examples of Pointers and One-Dimensional Array 1.1
Pointers and One-Dimensional Array 1.1

We have declared a pointer p assigned the address of the array.

(p + 1) means pointer is incremented by one means following array elements address which is 104.

*(p + 1) the values stored in 104, which is one, will get printed.

Example 2

The image below is example 1 on pointers and a one-dimensional array.

Examples of Pointers and One-Dimensional Array 2
Examples of Pointers and One-Dimensional Array 2

The above example is the same as example 1.

The only difference is the p is assigned to a + 1 address which is 104, as shown below.

Examples of Pointers and One-Dimensional Array 2.1
Pointers and One-Dimensional Array 2.1

*(p + 1) means the value stored in 108, which is two, will get printed.

Example 3

The image below is example 3 on pointers and a one-dimensional array.

Examples of Pointers and One-Dimensional Array 3
Examples of Pointers and One-Dimensional Array 3

In the above example, &a + 1 plays an important role.

Pointer assigned to &of the array means the complete array will get considered.

&a + 1 means the memory location next to the array.

That is 121 memory location.

The memory locations 100 to 120 are under the control of this array, but 121 is out of the bound.

So, the printf statement will print garbage value.

The third example is very important to understand.