-->

program to use 2d array/string to display address and values.

using codeblocks
---------------------------------------------------------------------------------------------------
//understanding array of pointers  for strings     //title of program
#include <stdio.h>                    //header file
int main()
{
     char *a[4];                      //pointer to store 4 addresses
    int i;
    char naam[4][6];                   //2d array of string
    printf("enter array elements\n");
    for(i=0;i<=3;i++)
    {
        printf("enter name for location %d\n",i);
        scanf("%s",naam[i]);                            // gets array elements
    }

    printf("the addresses are\n");
    for(i=0;i<=3;i++)
    {
         a[i]=&naam[i];                                // stores address of element of array
        printf(" location =%d and address=%d\n",i,a+i);//prints the address
    }
    printf("the values are\n");
    for(i=0;i<=3;i++)
    {
        a[i]=&naam[i];                                 // stores address of element of array
        printf(" location =%d and name=%s\n",i,*(a+i));//prints the stored value
    }
    return 0;
}

//note:-
//1. in 2d string of array, a=naam does not work. We have to work manually by assigning address as shown above.
//we can not print values or addresses using a[i]. it does not work.
-----------------------------------------------------------------------------------------------------
using turbo c++
---------------------------------------------------------------------------------------------------
//understanding array of pointers  for strings     //title of program
#include <stdio.h>                    //header file
#include<conio.h>                  //header file
int main()
{
     char *a[4];                      //pointer to store 4 addresses
    int i;
    char naam[4][6];                   //2d array of string
    printf("enter array elements\n");
    for(i=0;i<=3;i++)
    {
        printf("enter name for location %d\n",i);
        scanf("%s",naam[i]);                            // gets array elements
    }

    printf("the addresses are\n");
    for(i=0;i<=3;i++)
    {
         a[i]=&naam[i];                                // stores address of element of array
        printf(" location =%d and address=%d\n",i,a+i);//prints the address
    }
    printf("the values are\n");
    for(i=0;i<=3;i++)
    {
        a[i]=&naam[i];                                 // stores address of element of array
        printf(" location =%d and name=%s\n",i,*(a+i));//prints the stored value
    }
   getch();
    return 0;
}

//note:-
//1. in 2d string of array, a=naam does not work. We have to work manually by assigning address as shown above.
//we can not print values or addresses using a[i]. it does not work.

No comments:

Post a Comment