Dynamic String Arrays

I'm writing a code that can add a name to a list of pre-existing names or remove a name from the same list. Everything works fine except when I try to add a name. This is the function that I'm using to add a name:

1
2
3
4
5
6
7
8
9
10
string* addEntry(string* holder, int& size, string newEntry){
	size++;
	string* addNew;
	addNew=new string[size];
	for(int i=0;i<size-1;i++){
		addNew[i]=holder[i];//for some reason, addNew[i] doesn't change here
	}
	addNew[size]=newEntry;//addNew[size]=<Bad Ptr>
	return addNew;
}

The delete function works perfectly, but for some reason when I try to make addNew[i]=holder[i], addNew doesn't change at all and addNew[size] says that it contains a bad pointer. Am I doing something wrong? Because it looks like it would work to me. I tried creating a new project and pasting it to see if that was what was wrong, but still nothing. Any help?
Last edited on
addNew[size] is out of bounds. addNew[size-1] is the last element so it should be
addNew[size-1]=newEntry;
Last edited on
I can't believe I didn't realize that . . .
I've been messing with this for the past four hours and never remembered that index starts at 0, so the last element would be size-1. Fail.
Thanks you very much for your help
Topic archived. No new replies allowed.