Jump to content

C++ Pointer Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
c++leaner123

c++leaner123

    Newbie

  • Members
  • Pip
  • 2 posts
Hello. I am working on a program that implements a hash in the form of an array. I have the data structure set up but I am having a problem I cannot figure out with my pointers.

When I debug this in MS VS 2005 in class Has, List lis pointers(front and back) are initialized to 0 but each list in the has[10000] array pointers are not initialized. I cannot figure why this is happening. Any pointers? (pun intended).

#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

class Node{
public:
	string value;
	Node *next;
	Node():value(""), next(NULL){}
};
class List{
public:
	Node *front;
	Node *back;
	List() {front = new Node(), back = new Node();}
};
class HNode{
public:
	List l;
	HNode():l(){}/*not sure if this constructor is right for the list in this node*/
       ///the line above was HashNode, changed to Hnode///
};
class Hash{
public:
        List lis;
	HNode *has[10000];
	Hash(){has[10000];}
	void addone(string s){
		has[100]->l.enqueue(s);
	}
};
int main(){
        Hash hashdic;
	string add="successfully";
	hashdic.addone(add);
        return 0;
}

Spark notes: When the code is ran there is a Hash dic with a list lis with pointers initialized to 0xccccccc. Each instance in the has[10000] array has a list with pointers initialized to "???" Help please.

Edited by c++leaner123, 11 March 2009 - 08:44 PM.
changed constructor


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Observations:
1) HNode doesn't have a defined constructor.
2) Hash's constructor doesn't initialize its data members (such as the pointers to HNodes)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
c++leaner123

c++leaner123

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks for the response.

1) I mistyped my constructor in HNode. Would that be correct? I tried initializing the pointers by this way: l->front = NULL, l->back = NULL, and I recieved an error.

2) I am not sure how to initialize an array of pointers like this. Do I say HNode **has[1000]? Would I iterate through the whole array initialize it like that? I don't have my code on me so I cannot try it at the moment. I tried google but found nothing.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would initialize the array using a for loop.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog