Pointer to an Array in C1 min read

The basic idea of Pointer to an Array is that a pointer is used in with array that points to the address of the first element of that array.

Before we begin, you need to have the knowledge of following C programming:


Consider the following:

In the above example, the variable arr will provide the base address, which is a constant pointer pointing to the first element of the array that is to arr[0]. And hence the arr will contain the address of arr[0]. Thus, the above program fragment assigns ptr with the address of the arr.

Once the address of the first element is stored in the pointer ‘p’, we can access other elements of array elements using *p, *(p+1), *(p+2), and so on.

Now lets apply it in a programs.

Example 1: Array and Pointers in C

Output:


Example 2: Array and Pointers in C

Output:

In the above example array(arr) 4th element’s address is assigned to the pointer(ptr) and through that, we can print the value that the pointer points to by simply incrementing decrementing the pointer value such as by using ptr+1 or ptr-2, etc.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More