Q20 Write a program to implement pointer to array.

Admin

Write a program to implement pointer to array.

#include <stdio.h>
int main () 
{
   double balance[5] = {1000.0, 2.0, 3.4, 17.0,50.0};
   double *p;
   int i;
   p = balance;
   printf( "Array values using pointer\n");
   for ( i = 0; i < 5; i++ ) {
      printf("*(p + %d) : %.2f\n",  i,*(p + i) );
   }
   printf( "Array values using balance as address\n");
   for ( i = 0; i < 5; i++ ) {
      printf("*(balance + %d) : %.2f\n",  i,
 *(balance + i) );
   }
   return 0;
}

Output

Array values using pointer
*(p + 0) : 1000.00
*(p + 1) : 2.00
*(p + 2) : 3.40
*(p + 3) : 17.00
*(p + 4) : 50.00
Array values using balance as address
*(balance + 0) : 1000.00
*(balance + 1) : 2.00
*(balance + 2) : 3.40
*(balance + 3) : 17.00
*(balance + 4) : 50.00

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.