C Program to Access Array Elements Using Pointer

Array element by increment pointer in c. When using pointers for arrays, the pointer’s data type must match the array’s data type. To get the next spot, the increment operation is performed on the pointer variable in the loop. This arithmetic applies to all array sizes. despite the fact that each data type consumes a different number of bytes

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

 

Output:

Address of var[0] = 457265988

Value of var[0] = 10 Address of var[1] = 457265992

Value of var[1] = 20 Address of var[2] = 457265996

Value of var[2] = 30

Pointer in C Program

A pointer is a variable that stores the address of a memory location. Pointers are used to keep track of the addresses of other variables or memory items. Pointers are extremely useful for another type of parameter passing known as Pass By Address.

It usually depends on the underlying processor’s word size; for example, on a 32-bit computer, the pointer size can be 4 bytes; on a 64-bit computer, the pointer size can be 8 bytes.

Pointer Increment in C

When a pointer is incremented, the number equal to the size of the data type to which it is a pointer is actually increased. It’s a unary operator that returns the variable’s value at the address specified by its operand.

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.

Return Statement to Execute Array Element by Increment Pointer in C

A return statement in computer programming causes execution to exit the current subroutine and resume at the point in the code immediately after the instruction that called the subroutine, known as its return address. 

The return statement returns the execution flow to the function from which it was called. This statement does not require any conditional statements. When the statement is executed, the program’s flow stops immediately and returns control to where it was called.