Advertisement

05.03.2008 at 10:52PM PDT, ID: 23374578
[x]
Attachment Details

initialize array of char pointers using dynamic memory allocation

Asked by dandeliondream in C++ Programming Language

How do I initialise an array of char pointers using dynamic allocation? I'm trying to do this assignment using just pointers and dynamic allocation. Create the array of char pointers, assign values to them and print out the values. Is there an easier way to do this? Below is what I've done...Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
#include <iostream>
using namespace std;
 
int main()
{
    
    //array of char pointers
    char *fruit[3];
    
    for ( int i = 0; i < 3; i++)
        fruit[i] = new char[30];
    
    char *start, *end;
    for (start = fruit, end = &fruit[2]; start <= end; start++)
    {
        cin >> start;
    }
    cout << endl;
    for (start = fruit, end = &fruit[2]; start <= end; start++)
    {
        cout << start << endl;
    }
        
    for ( int i = 0; i < 3; i++)
        delete [] fruit[i];
        
    system("PAUSE");   
     
    return 0;
}
[+][-]05.03.2008 at 11:49PM PDT, ID: 21494764

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.04.2008 at 12:15AM PDT, ID: 21494809

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C++ Programming Language
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.04.2008 at 12:46AM PDT, ID: 21494866

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.04.2008 at 12:50AM PDT, ID: 21494881

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628