-->

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;
    int *p;
    p=a;                               //assigns base address to pointer
    printf("the address of array=%d\n",p);//prints address of array
    p=&a[0];                            //assigns address of zeroth element
    printf("the address of zeroth element of array=%d\n",p);//prints address of zeroth element
    printf("all addresses are\n");
    for(i=0;i<=4;i++)
    {
        printf("location=%d and address=%d\n",i,p+i);//it prints address of respective elements
    }
     printf("values  are\n");
    for(i=0;i<=4;i++)
    {
        printf("location=%d and address=%d\n",i,*(p+i));//it prints value of respective 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;
    int *p;
    p=a;                               //assigns base address to pointer
    printf("the address of array=%d\n",p);//prints address of array
    p=&a[0];                            //assigns address of zeroth element
    printf("the address of zeroth element of array=%d\n",p);//prints address of zeroth element
    printf("all addresses are\n");
    for(i=0;i<=4;i++)
    {
        printf("location=%d and address=%d\n",i,p+i);//it prints address of respective elements
    }
     printf("values  are\n");
    for(i=0;i<=4;i++)
    {
        printf("location=%d and address=%d\n",i,*(p+i));//it prints value of respective location
    }
   getch();
    return 0;
}


No comments:

Post a Comment