Help with char pointers

Hello,

Thanks for your help.

I'm at a conceptual roadblock with my study of char pointers. I am instructed to create arrays of char pointers in the Sentence class, and then fill them in with words in the constructor. I will need to call on these words later, possibly to print directly and definitely to fill in information in a derived class called Book(not created yet).

Right now, I want to just get the array of char pointers populated with the words you can see here, and printed through the print() function. I believe once I get past this step the rest is within my knowledge scope.

I realize this program is not syntactically correct. As you can see I have attempted several different ways to solve this problem. Here is what I have right now:

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
31
32
33
#include <iostream>
using namespace std;

const int numOfWords = 5;

class Sentence
{
	
	const char * articles[]={"the","a","one","some","any"};
	//const char* articles		[numOfWords];
	const char* noun			[numOfWords];
	const char* verb			[numOfWords];
	const char* preposition	[numOfWords];

public:
	Sentence()
	{
		const char * articles[]={"the","a","one","some","any"};
		//char ** articles[]={&temp[0],&temp[1],&temp[2],&temp[3],&temp[4]};
	}
	void print()
	{
		//const char* articles[]={"the","a","one","some","any"};
		cout << *articles[2];
	}
};

int main ()
{
	Sentence s1;
	s1.print();
	return 0;
}


Where am I going wrong?

Thanks.
If you work with arrays and char pointers, you'll have a hard time. First, you won't be able to resize the arrays, secondly, if you ever want to put something other than string literals, you'll have to deal with memory management.

The right way is to have a vector<string> words;. You'll need to include <vector> and <string> headers. I'm not sure which members of your class should be constant and which are going to be dynamic, so I'll give an unrelated example:
1
2
3
4
5
6
7
vector<string> foo;//creates an empty array.
//you could write foo(5) to create an array of 5 elements.
foo.push_back("hello");//expands the array with a new string
foo.resize(7);//creates 6 more empty strings
foo[4] = "world";//you can use vectors almost like arrays
for(int i = 0; i < foo.size(); i++) // use size() or length() to find how many strings you have
   cout << foo[i] << '\n';

See http://cplusplus.com/reference/stl/vector/
Last edited on
THanks for the response. I am instructed to write this program using these methods. I must create a Sentence class with arrays of char pointers for the different types of words, and I must "fill them in"(his words) in the constructor. We have not learned vector.
Okay, then you declare the array as const char * articles[5]; and in your constructor do articles[0] = "the"; and etc. There is no prettier way, unless you can make articles static..
Thanks again. That's what I ended up doing. Once I removed the {} and replaced with what you mentioned everything worked fine. Amazed there is not a prettier way, but I suppose that is what these vector features you and others have explained to me on this question are there for.
Topic archived. No new replies allowed.