You are here

C++ - Pointer To Pointer Array

#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    char *str1 = new char[10];
    char *str2 = new char[5];
    char *str3 = new char[10];
 
    for (int i=0; i<9; i++)
    {
        str1[i] = 'a';
        str3[i] = 'c';
    }
    str1[9] = 0;
    str3[9] = 0;
    for (int i=0; i<4; i++)
    {
        str2[i] = 'b';
    }
    str2[4] = 0;
 
    char **strArray = new char*[3];
    strArray[0] = str1;
    strArray[1] = str2;
    strArray[2] = str3;
 
    cout << strArray[0] << endl;
    cout << strArray[1] << endl;
    cout << strArray[2] << endl;
 
    /*******************************************/
    char **strArray2 = new char*[3];
    strArray2[0] = new char[10];
    strArray2[1] = new char[5];
    strArray2[2] = new char[10];
 
    for (int i=0; i<9; i++)
    {
        (strArray2[0])[i] = 'a';
        (strArray2[2])[i] = 'c';
    }
    (strArray2[0])[9] = 0;
    (strArray2[2])[9] = 0;
    for (int i=0; i<4; i++)
    {
        (strArray2[1])[i] = 'b';
    }
    (strArray2[1])[4] = 0;
 
    cout << strArray2[0] << endl;
    cout << strArray2[1] << endl;
    cout << strArray2[2] << endl;
 
    return 1;
}