C++ program to illustrate the use of array of pointers

#include<iostream>
#include<string>
using namespace std;
int main()

{

             char *ptr[10];

             char temp[20];

             int n,m;

             cout<<"Enter how many names :";

             cin>>n;

             for(int i=0;i<n;i++)

             {

                 cin>>temp;

                 m=strlen(temp);

                 ptr[i]=new char[m+1];

                 strcpy(ptr[i],temp);

             }

             for(i=0;i<n;i++)

             cout<<endl<<ptr[i];

             for(i=0;i<n;i++)

             delete ptr[i];

             return 0;

}

Input

Enter how many names : 3

Sartaj

Sahni

Galvin

Output

Sartaj

Sahni

Galvin

Leave a Reply

Your email address will not be published.