Program in C and C++ to accessing array elements in different ways
C
CPP
#include<stdio.h> int main() { int a[10]={1,2,3,4,5,6,7,8,9,10},i; printf("1st way a[i] "); for(i=0;i<10;i++) printf("%4d",a[i]); printf("\n2nd way i[a] "); for(i=0;i<10;i++) printf("%4d",i[a]); printf("\n3rd way *(a+i)"); for(i=0;i<10;i++) printf("%4d",*(a+i)); printf("\n4th way *(i+a)"); for(i=0;i<10;i++) printf("%4d",*(i+a)); return 0; }
#include<iostream> using namespace std; int main() { int a[10]={1,2,3,4,5,6,7,8,9,10},i; cout<<"1st way a[i] "; for(i=0;i<10;i++) cout<<" "<<a[i]; cout<<"\n2nd way i[a] "; for(i=0;i<10;i++) cout<<" "<<i[a]; cout<<"\n3rd way *(a+i)"; for(i=0;i<10;i++) cout<<" "<<*(a+i); cout<<"\n4th way *(i+a)"; for(i=0;i<10;i++) cout<<" "<<*(i+a); return 0; }
Output:-
0 Comments