# null pointers

• 03-29-2002
ygfperson
null pointers
Code:

```class atom{ int protons; int electrons; int neutrons; public: atom* bond[4]; private: friend int bind(atom&,atom&); public:  atom(int x) {   protons=x;   electrons=x;   neutrons=x;   for (int i=0;i<4;i++) bond[0]=NULL;  }  int type() { return protons; }  int get_group() {   int x=electrons;   if (!x) return -1;   if (x>=114) return -1;   if (x==1) return 1;   if (x==2) return 8; x-=2;   for (int i=0;i<2;i++)  if (x<=8) return x;      x-=8;   for (int i=0;i<2;i++) {   if (x<=2) return x; x-=2;   if (x<=10) return -2; x-=10;   if (x<=6) return x+2; x-=6;   }   for (int i=0;i<2;i++) {   if (x<=2) return x; x-=2;   if (x<=14) return -2; x-=14;   if (x<=10) return -2; x-=10;   if (x<=6) return x+2; x-=6;   }   return -1;  } }; int bind(atom& x,atom& y) {  int count=0; int count_1=0;  if ((x.bond[3]!=NULL) || (y.bond[3]!=NULL)) return -1;  while (x.bond[count++] != NULL);  while (y.bond[count_1++]!=NULL);  x.bond[count]=&y;  y.bond[count_1]=&x; } int main() {  atom* atoms[5];  atoms[0]=new atom(CARBON);   for (int i=1;i<5;i++) {   atoms[i]=new atom(HYDROGEN);   bind (*atoms[0],*atoms[i]);   }   cout << "Protons: " << atoms[0]->type() << endl;   cout << atoms[0]->bond[0]->type() << endl;   cout << atoms[0]->bond[1]->type() << endl;   cout << atoms[0]->bond[2]->type() << endl;   cout << atoms[0]->bond[3]->type() << endl;  return 0; }```
for some reason, when i try to print the types of the atoms that are bonded (the last 4 couts) it gives me an error. i'm almost certain the problem is because bond[0] is a NULL pointer. the bond[0] pointer should have been set by the bind function. can anyone spot the error?
• 03-29-2002
Sorensen
I stopped at -

bond[0]=NULL;

should be

bond[i]=NULL;

• 03-29-2002
swoopy
```>int bind(atom& x,atom& y) { > int count=0; int count_1=0; > if ((x.bond[3]!=NULL) || (y.bond[3]!=NULL)) return -1; > while (x.bond[count++] != NULL); > while (y.bond[count_1++]!=NULL); > x.bond[count]=&y; > y.bond[count_1]=&x; //Change the previous two lines to:  x.bond[--count]=&y;  y.bond[--count_1]=&x; //Or modify the while-loops above }```