Tuesday, December 21, 2010

How the compiler treats an array internally ??

Solution: Let us suppose a 2 D  Array of type interger defined by the user as given below:
                          int array[5] [10] ; --------- 1)
   
  Complier will treat an array internally as given below:
                         int *(*(p+5)+10);

  However one cant declare an array  as the compiler treates it internally... Any1 can have try on it and compiler will give error . e.g
                       int *(*(p+5)+10)  will give an error while declaring array.

However You can use such expressions in sc panf and printf statements to enter and access elements of array.
Example:
#include<stdio.h>
#include<conio.h>
int func(int *,int);
  int arr[]={1,2,3,4,5};
int main()
{
    func(arr,5);
   getch();
   return 0;
}
int func(int *a,int z)
{
   int i;
   for(i=0;i<z;i++)
   {
      printf("%d\n",*(arr+i)); // making use of the concept i hav discussed above
    }
}

No comments:

Post a Comment