Access Array Elements Using Pointers in C

Array of Pointers in C Language. An array of pointers is an indexed set of variables with pointers as variables. In computer science, pointers are used to create, use, and delete various types of data structures. An array of pointers serves the same purpose as any other array: it helps you to numerically index a large number of variables.

#include <stdio.h>
const int MAX = 3; 
int main()
{
int var[]={10, 20, 30}; 
int i = 0;
int *ptr[MAX];
for(i = 0; i < MAX; i++)
{
ptr[i] = &var[i];
}
for(i = 0; i < MAX; i++)
{
printf("Value of variable[%d] = %i\n\n", i, *ptr[i]);
}
return 0;
}

 

Output:

Value of variable[0] = 10

Value of variable[1] = 20

Value of variable[2] = 30

const int* Pointer in C

The const int* const specifies that the pointer can point to a constant int and that the value of the int pointed by this pointer cannot be changed. We also can’t change the value of the pointer because it’s now constant and can’t point to another constant int. 

int const* is a constant integer pointer. This means that the declared variable is a pointer to a constant integer. This effectively means that the pointer is pointing to a value that should not be altered.

Pointers to Execute Array of Pointers in C Language

For file handling, pointers are used. Pointers are used to dynamically allocate memory. 

A pointer declared to a base class in C++ could access an object of a derived class.

A pointer to a derived class, on the other hand, cannot access the object of a base class. 

In C, a pointer is a variable that stores the address of another variable. 

This variable can be of any type, including int, char, array, function, or pointer. 

The pointer’s size is determined by the architecture. However, in 32-bit architecture, a pointer is 2 bytes in size.

For Loop in C

A for loop is a type of repetition control structure that allows us to write a loop that is executed a set number of times. 

The loop allows us to perform a n number of steps in a single line. 

A for-loop (or simply for loop) is a control flow statement in computer science that specifies iteration and allows code to be executed repeatedly.

The term for-loop is derived from the word for, which is used as a keyword in many programming languages to initiate a for-loop.