Hello,

I have problem witch i've been working on for a couple of days now with no real progress. The problem is that i'm trying to search trough a array of pointer for a certain value. If the value exist return that value to be used in another function, but if it doesn't exist return NULL. It's a simple if else statement, but the else part is not working correctly. The problem is with the function " link* symbolTable::lookupArray(string value)". If the value is in the symbol table it works fine, but not if the value is not present. It compiles fine, but i get a error screen and have to close the window. I'm using ms visual studio. Here my code brief.

struct link
{
	string data;
	int token;
	link *next;
};

class key  
{
protected:
	link *first;
public:
	key (): first(NULL){}
	~key () {} // destructor
	void addTailItem (string);
	void addHeadItem (string);
        void findItem ();
	void displayItem ();
	void fillItem ();
	int hashfunction (string); // hash value from string
};

class symbolTable : public key // replace keyword with key
{
private:
	static const int MAX_LEX_ARRAY = 999;	// size of lexeme array
	static const int MAX_SYM_ARRAY = 9948;	// size of symbol table array prime 9949
        link *symbol[MAX_SYM_ARRAY];			// pointer table array
	string lexemeArray[MAX_LEX_ARRAY];		// array holding the lexemes
	
public:
	symbolTable() {}
	void loadSymbol ();				 // add keywords to symbol table
	link* lookupArray(string);			// find function for symbol table
	void insertArray (string);			// insert function for symbol table
};

void symbolTable::loadSymbol()
{
	cout<<":::loadsymboltable::: "<<endl;
	link* current = first;
	
	while(current != NULL)
	{
		if (current == NULL)
		{
			current = current->next->next;
		}
		
		symbol [hashfunction(current->data)]= current; 
		current = current ->next;
	}
}

link* symbolTable::lookupArray(string value)
{
	link* l;
	
        for (l = symbol[hashfunction(value)]; l != NULL; l = l->next)
	{
		if (l->data == value)
		{
			return l;
		}
		else                      //THIS ELSE STATEMENT DOES NOT WORK
		{
			return NULL;   
		}
	}
}

any help at all would be appreciated

Ancient Dragon commented: Thanks for using code tags :) +21

Recommended Answers

All 4 Replies

> if (current == NULL) // in loadSymbol
1. current can never be NULL at this point in the code, given that it's the first thing after the inverse test in the while loop.
2. It's a good job anyway, because the code inside the if() would dereference a NULL pointer.

> It compiles fine, but i get a error screen and have to close the window.
> //THIS ELSE STATEMENT DOES NOT WORK
It would seem to me that your list doesn't end with a NULL pointer, but instead a junk pointer. So it just follows the garbage through memory until it steps on a proverbial land mine and blows up.
Check your code which creates the list to make sure it really does end with some ->next = NULL.

Salem,

Thanks for the reply. I removed the if statement, it seems you are right, current can indeed never be null at that point so it's not needed. I've been working on what you said about my list never ending with a null pointer. So I started by filling my symboltable array with null values via a for statement. Next I loaded my linklist in the symboltable array. Now I do not get an error, but I want to have proof via a cout statement that the else statement is being accessed when there's no value matching the string i'm looking for. But the cout statement never comes in the output window

void symbolTable::emptySymbol ()
{
	for (int i  = 0; i < 9948; i++)
		symbol [i]= NULL;
}
void symbolTable::loadSymbol()
{
	link* current = first;
	for (; current != NULL; current = current->next)
		symbol [hashfunction(current->data)]= current; 
}

link* symbolTable::lookupArray(string value)
{
	link* l;
	
	for (l = symbol[hashfunction(value)]; l != NULL; l = l->next)
	{
		if( l->data == value)
		{
			cout<<"\nFound it: "<<l->data<<endl;
			return l;
		}
		else
		{
			cout<<"I'm in the else statement: "<<endl; // NEVER DISPLAYS
			return NULL;
		}
	}
}

Any idea's anyone??

Do you need the else statement there? The way you currently have it set up if l->data != value the first time through the loop, the loop will stop, and you will never advance to l->next. If l->data == value the first time through the loop you will never get to the else because of the return statement in the body of the if statement.

I'd take out the else statement where it's currently located, let the loop run, and check the return value of l back in the calling function. If value is found in the loop then value should be available in the calling function. If value isn't found in the calling function, then l became NULL when the loop stopped, and you check for that in the calling function.

if(lookupArray(desiredString))
  cout << "desired string was found" << endl;
else  //means the return value was NULL 
  cout << "desired string was not found" << endl;

Problem finally solved. Basically the function return the value in the if statement if it is present in the array, if not it returns NULL that's located in the body of the function. The else statement was not needed. Thanks to everyone who helped me out on this one.

link* symbolTable::lookupArray(string value)
{
	link* l;
		
	for (l = symbol[hashfunction(value)]; l != NULL; l = l->next)
	{
		if (value == l->data)
		{
			cout<<"YES: "<<symbol[hashfunction(l->data)]->data<<endl;
			return l;
		}
	}
	cout<<"NO: "<<value<<endl;
	return NULL;
}
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.