I have an array that I need to make into an array of pointers and return the array.

string words[i] = {
                            "when",
                            "if",
                            "as if",
                            "in order that",
                            "before",
                            "because",
                            "as soon as",
                            "after",
                            "although",
                            "as long as",
                            "since",
                            "unless",
                            "even though",
                            "so that",
                            "while",
                            "not even"
							};

Recommended Answers

All 4 Replies

try
string *array[arraysize];

then to initialize array[0] = pointer to string
etc....

to derefence *array[0];

you are close -- just leave out the i -- do not specify the number of words but let the compiler figure it out from the number of initializers. And BTW this is NOT an array of pointers but an array of std::string objects. There is a huge difference.

string words[] = {
                            "when",
                            "if",
                            "as if",
                            "in order that",
                            "before",
                            "because",
                            "as soon as",
                            "after",
                            "although",
                            "as long as",
                            "since",
                            "unless",
                            "even though",
                            "so that",
                            "while",
                            "not even"
							
};

sorry im new to the pointer thing. this is what I have
it is not working.

string subordanate()
	{
	string *words[];
	words[] = {
                            "when",
                            "if",
                            "as if",
                            "in order that",
                            "before",
                            "because",
                            "as soon as",
                            "after",
                            "although",
                            "as long as",
                            "since",
                            "unless",
                            "even though",
                            "so that",
                            "while",
                            "not even"
							};
	for (i = 0; i < 16; i++)
		return words[];

	}

You can't return an array of pointers, but you can put all the pointers in a structure an return a pointer to it.

struct sarray
{
    string* words;
};

struct sarray* subordanate()
{
    struct sarray* sa = new sarray;
    sa->words = new string[16];
    sa->words[0] = "when";
    sa->words[1] = "if",
    sa->words[2] = "as if",
    sa->words[3] = "in order that",
    sa->words[4] = "before",
    sa->words[5] = "because",
    sa->words[6] = "as soon as",
    sa->words[7] = "after",
    sa->words[8] = "although";
	return sa;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.