-->

program to understand array as pointer

using codeblocks
-------------------------------------------------------------------------------------------------------
//understanding array as pointer      //comment of program
#include <stdio.h>                     //header file
int main()
{
    int a[]={2,3,4,5,6};                 //array with some values
    int i;
    printf("the values are\n");
    for(i=0;i<=4;i++)
    {
        printf("location =%d and value=%d\n",i,a[i]);//prints value without pointer
    }
    printf("printing value using array as pointer\n");
    for(i=0;i<=4;i++)
    {
        printf("location =%d and value=%d\n",i,*(a+i));
        //prints value with pointer. 'a' is an array; 'i' is for location.
        //To display value, we use '*' with location; as shown here.
    }
    printf("printing address using array as pointer\n");
    for(i=0;i<=4;i++)
    {
        printf("location =%d and value=%d\n",i,(a+i));//prints address of different location
    }
    return 0;
}
----------------------------------------------------------------------------------------
using turbo c++
---------------------------------------------------------------------------------------------------
//understanding array as pointer      //comment of program
#include <stdio.h>                     //header file
#include<conio.h>
int main()
{
    int a[]={2,3,4,5,6};                 //array with some values
    int i;
    printf("the values are\n");
    for(i=0;i<=4;i++)
    {
        printf("location =%d and value=%d\n",i,a[i]);//prints value without pointer
    }
    printf("printing value using array as pointer\n");
    for(i=0;i<=4;i++)
    {
        printf("location =%d and value=%d\n",i,*(a+i));
        //prints value with pointer. 'a' is an array; 'i' is for location.
        //To display value, we use '*' with location; as shown here.
    }
    printf("printing address using array as pointer\n");
    for(i=0;i<=4;i++)
    {
        printf("location =%d and value=%d\n",i,(a+i));//prints address of different location
    }
   getch();
    return 0;
}

No comments:

Post a Comment