# Problems with arrays/pointers



## Swiftle (Feb 27, 2010)

I'm testing an array of pointers that stores Objects. This is the class:


```
class Entry{

public:
	void show();
	int page;
	char *name;
};
```

In my main I have declared 


```
Entry **v;
```

In order to get my data, I read from a file using 


```
ifstream file;
	file.open(words);
	while(!file.eof()){
		file.getline(url, 100);		
		
			
			v[indx] = new Entry();
			v[indx]->name = url;
			v[indx]->page = indx+10;	

			indx++;
		
	}
```

Now here's the thing: If I try to display the information stored in v_ I only get the page but not the name. However if I display the info inside the while I get both the name and the page. Now this bothers me because I have to make a function that checks if I'm not inserting something that's already in v. For this I use: 



		Code:
	

bool isPresent(Entry **v, char* name){
	string str1 = name;
	bool present = false;
	for(int i = 0; i < indx and !present; i++){
		string str2 = v[i] -> name;

		if(str1.compare(str2) == 0)
			present = true;	
	}
	return present;
}


but str2 always has the same thing as str1 meaning I cannot insert anything past the first entry.  I think it's because it can't access  v ->info._


----------



## Swiftle (Feb 27, 2010)

Actually I think I found my error; I'm creating an Entry which has url as name. So name points to url, but because url changes after every getline I'm getting my errors which mean that I need to store all my urls somewhere first.


----------



## W1zzard (Feb 27, 2010)

did you allocate memory for **v ? do you allocate memory for name then creating the object ?

char* foo="bar";
char* baz=foo; // does not copy the contents of the string, it just copies the pointer
// correct way to do it
char* baz=strdup(foo);
// do stuff with baz
free(baz); baz=NULL;

look at strdup, strcpy
strcpy is bad(tm) because it enables buffer overflows, look at strncpy or strcpy_s or whatever your c library gives you that is more secure

ALWAYS initialize your pointers to NULL so you can spot such errors easily

in order to detect "end" of your array the most common way is to add a NULL pointer element at the end so you can do while (foo[bar]!=NULL) {

and don't forget to release your memory when you are done with the object(s)


----------



## Swiftle (Feb 27, 2010)

In my declaration of Entry, I changed the char *name to string name: solves my problems


----------



## W1zzard (Feb 27, 2010)

imo it's better to learn about the cause of a problem and fix it, not fix it with something that magically works without understanding why


----------



## Swiftle (Feb 27, 2010)

But I found the error ! My Entry class had this member: char *name. A pointer to a char. During the reading of the textfile I assigned the value from my getline() to the Entry->name. But getline() changes after each line and in the end disappears. Since I wasn't storing the contents of the getline() anywhere in the end my Entry->name points to nothing. If I stored the getlines() in a matrix for exemple and then assign a line of that matrix to Enrty -> name it would work. By replacing char *name with string name I have a place to store the value of name. Not sure if I'm very clear but I think this was my problem


----------



## Swiftle (Feb 28, 2010)

Just saw it was void generateDictionnary() instead of void Dictionnaire::generateDictionnary -_- I'm done coding past 2AM.


----------

