-->
Showing posts with label program to understand array as pointer. Show all posts
Showing posts with label program to understand array as pointer. Show all posts

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;
}


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;
}