Problem 57

author
By Supreme2023-03-16
Problem Statement :
Write a program to access elements using pointer.
Code:

#include <stdio.h>

int main() {

//57. Write a program to access elements using pointer.

printf("\tElements Of Array\n");
printf("===================================\n\n");

int arr[5] = {18, 19, 20, 21, 22};
int *ptr;
int i;

ptr = arr;

for (i = 0; i < 5; i++) {
printf("\nThe value of element %d is: %d\n", i, *(ptr + i));
}

return 0;
}

Description :
  • This is a program written in C language that demonstrates how to access array elements using a pointer.
  • The program includes the standard input-output library stdio.h.
  • The main() function is defined.
  • Two variables of int data type are declared: arr which is an array of 5 integers, and ptr which is a pointer to an integer.
  • An integer variable i is declared.
  • The pointer ptr is initialized to the base address of the array arr.
  • A for loop is used to iterate over the array. The loop variable i is used to index each element of the array.
  • The value of each element in the array is accessed using the pointer ptr. The expression *(ptr + i) gives the value of the i-th element of the array.
  • The value of the i-th element is printed using printf() function.
  • Finally, the main() function returns an integer value 0.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.