Simple for others , not for me.
How do you allocate memory for an array of pointers, each pointer pointing on a string.
The problem is , that the size of the array could change depending on the number of strings the user wants to enter.
This needs to be done in a function.
There are other functions needed to be written from the main.
At least get me started

In c++ you have the choice of using either new operator or malloc() function to allocate that memory. new is preferred in c++ but malloc() may be a better solution when you need to change the size of the array while the program is running.

An array of pointers to strings that can be resized at any time is declared like this: char **array. to allocate memory for 20 strings

char **array = (char **)malloc(20 * sizeof(char **));

now, if you want to expand it to 30 strings

array = (char **)realloc(array, 30 * sizeof(char **));

Similar thig can be done using new but you have to write your own realloc() function.

What do you meen "my own realloc"
And please how do you declare and define the pointer.
The program needs to have a main and functions

Just use a vector of strings.

I would normally agree but I don't think the OP has that option. This may be an assignment that teaches pointers, not vectors.

Member Avatar for iamthwee

I would normally agree but I don't think the OP has that option. This may be an assignment that teaches pointers, not vectors.

Ah yes, you are probably right.

What do you meen "my own realloc"

c++ does not have the equivalent of the realloc() function, which allocates a new pointer, copies the data from the old pointer to the new pointer, then deletes the old pointer. And you can't use realloc() if you used new. So you would have to write your own function that does the same thing as realloc() but uses the new and delete operators.

And please how do you declare and define the pointer.

I already showed you that in my previous post. char **array declares an array of pointers to strings, which is the purpose of the two asterisks.

The program needs to have a main and functions

Yes, all (or most) c++ programs must have a main function.

Now, start writing your program and posting code if you need more help.

:icon_cheesygrin: you're a very witty dragon.

I ment to ask how do you send or get double pointer (if that's what it's called) from function to main.
i.e.
the main has an option of sending the pointer to get strings into it and has the option of sending the pointer to have strigs deleted.

if you declare the pointer in main() then just pass it by reference to the other functions that need it, just as you would any other parameter.

There are two ways you can do this: pass a pointer to the array -- see the tripple stars in the declaraction. That means its a pointer to a 2d array.

char **foo( char*** array, size_t size)
{
    *array = new char*[size];
}

int main()
{
    char **array = 0;
    foo( &array, 10 );
}

Or use the c++ reference operator &, which simplifies all those asterisks

char **foo( char**& array, size_t size)
{
    array = new char*[size];
}

int main()
{
    char **array = 0;
    foo( array,10 );
}
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.