C Program to Access Elements of an Array Using Pointer

@codevarsity
 · 
2.1k views
 · 

This sample C program declares the array of five element and the elements of that array are accessed using pointer.

Related Topics

Arrays  Pointers  for loop 

Code

/*C Program to Access Elements of an Array Using Pointer*/
#include <stdio.h>

int main()
{
   int data[5], i;
   printf("Enter elements: ");

   for(i = 0; i < 5; ++i)
     scanf("%d", data + i);

   printf("You entered: \n");
   for(i = 0; i < 5; ++i)
      printf("%d\n", *(data + i));

   return 0;
}

Output

Enter elements: 1
2
3
5
4
You entered: 
1
2
3
5
4

Explanation

C Program to Access Elements of an Array Using Pointer

In the structure complex, we are using float datatype for variables, real and imag since our concern is to add any two complex numbers with a possible fractional value.

In the main() function, the real part and the imaginary part of the complex numbers are taken separtely using scanf().

The structures n1 and n2 are passed as an argument of function add(). The function computes the sum and returns the structure variable temp to the main() function.

The final result is printed using printf() throught the main() function.

Tags

#for  #return