Array is the most important concept to learn in any programming language. Most of us started learning array from the C programming language. The simplest definition of array can be defined as below.
“An array is a collection variable which has common name shared by all its elements”
An array properties are, the array index starting from 0 not 1, array name must follows all the variable naming rules; the arrays are not self describing i.e. The length of an array cannot be remembered in array.
So, I assume that all of you already aware of array and various operations of an array. Here I give you simple puzzle on C language array which will helps you to learn array more dipper and also understand internal working of an array.
Let us start an array Puzzle,
Consider that we have declared an integer array in our program with name myarr with size 5 elements. We initialized an array myarr with values 1,2,3,4 and 5 respectively. To retrieve the array element the various methods can be used as given below.
1. myarr[index]
2. index[myarr]
3. *myarr+ index
4. index+*myarr.
Now the challenge to an expert programmer is that they have to post their response in comment to describe and give reason why all of above methods works with array?
Here below I included source code which implements above four methods in array.
#include<stdio.h>
void main()
{
int myarr[5]={1,2,3,4,5};
printf("%d\n",0[myarr]);
printf("%d\n",myarr[1]);
printf("%d\n",*myarr+2);
printf("%d\n",*myarr+3);
printf("%d\n",4[myarr]);
}
void main()
{
int myarr[5]={1,2,3,4,5};
printf("%d\n",0[myarr]);
printf("%d\n",myarr[1]);
printf("%d\n",*myarr+2);
printf("%d\n",*myarr+3);
printf("%d\n",4[myarr]);
}
The below link provide you online compiler where you can run your code and get output of program. This online compiler is provided by codepad.org.
0 comments:
Post a Comment