Subscribe to shafiq.chaudhry.cs' Blog        RSS Feed
-----

pointer to pointer to pointer to character string

Icon Leave Comment
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include <cstdlib>

#define PAGES 2
#define ROWS 4
#define COLS 20

//function is used to populate char *** array
//first param is 3 d pointer to character array
//2nd parameter's analogy is same a page of note book
//3rd paramter's analogy is same as rows in a page of note book
void fillArray ( char ***, const int, const int );

//function is used to display char *** array
//first param is 3 d pointer to character array
//2nd parameter's analogy is same a page of note book
//3rd paramter's analogy is same as rows in a page of note book
void displayArray ( char ***, const int, const int );


int main ()
{
    system ( "color 3e" );
    system ( "title Shafiq Hussain" );

    // ptr is a pointer to a pointer to a pointer to char
    char ***ptr;

    //                          Memory allocating
    //ptr points to PAGES time pointer to pointer to char
    ptr = new char ** [PAGES];

    for ( int page = 0; page < PAGES; page ++ )
    {
        //Each pointer to pointer  to char has ROWS times an array to char
        ptr [page] = new char * [ROWS];

        for ( int row = 0; row < ROWS; row ++ )
        {
            // each pointer to pointer to pointer points COLS times
            // characters in the memory
            ptr [page][row] = new char [COLS];
        }
    }

    // Now taking input from user
    // Two pages
    // Each page has 4 lines
    // Each line has 20 characters
    // I have to save all names in 2 pages//4 rows//20 chars

    fillArray ( ptr, PAGES, ROWS );

    // Displaying the pointer strings
    displayArray ( ptr, PAGES, ROWS );

    cout << endl << endl << endl;


    system ( "pause" );

    return 0;
}



void fillArray ( char ***ptr, const int pages, const int rows )
{
    for ( int page = 0; page < pages; page ++ )
    {
        cout << "\t\t\t\tData in for page#" << page + 1 << endl << endl;

        for ( int row = 0; row < rows; row ++ )
        {
            cout << "Please enter name for row#" << row + 1 << " : ";
            cin >> ptr [page][row];
        }
        cout << endl << endl;
    }
}

void displayArray ( char ***ptr, const int pages, const int rows )
{
    for ( int page = 0; page < pages; page ++ )
    {
        cout << "\t\t\t\tData out for page#" << page + 1 << endl << endl;

        for ( int row = 0; row < rows; row ++ )
        {
            cout << "In row#" << row + 1 << " name is: " << ptr [page][row] << endl;
        }
        cout << endl << endl;
    }
}


0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Tags

    Recent Entries

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories