I need to store a list with chemical elements and its properties. I have programmed a class like this:
class Elementos
{
public:
Elementos();
~Elementos();
int getZsymbol(char *Symbol);
.....
private:
enum {MaxElem=118,NameLen=16,NN=-999};
struct ElementData
{
int AtomicNumber;
char Symbol[3+1];
char SpanishName[NameLen+1];
char EnglishName[NameLen+1];
char GermanName[NameLen+1];
.....
}Propiedades[MaxElem],*p;
}
class Elementos
{
public:
Elementos();
~Elementos();
int getZsymbol(char *Symbol);
.....
private:
enum {MaxElem=118,NameLen=16,NN=-999};
struct ElementData
{
int AtomicNumber;
char Symbol[3+1];
char SpanishName[NameLen+1];
char EnglishName[NameLen+1];
char GermanName[NameLen+1];
.....
}Propiedades[MaxElem],*p;
}
To copy to clipboard, switch view to plain text mode
And in cpp file:
#include "elementos.h"
Elementos::Elementos()
{
ElementData Propiedades[MaxElem] =
{
{ 1, "H" , "Hidrógeno" , "Hydrogen" , "Wasserstoff" },
{ 2, "He", "Helio" , "Helium" , "Helium"}
...
};
p=&Propiedades[0];
}
int Elementos::getZsymbol(char *Symbol)
{
ElementData *q;
q=p;
string c = string(Symbol);
for (int i=0;i<MaxElem;i++){
string s = string(q->Symbol);
if (c==s) return q->AtomicNumber;
q++;
}
return 0;
}
#include "elementos.h"
Elementos::Elementos()
{
ElementData Propiedades[MaxElem] =
{
{ 1, "H" , "Hidrógeno" , "Hydrogen" , "Wasserstoff" },
{ 2, "He", "Helio" , "Helium" , "Helium"}
...
};
p=&Propiedades[0];
}
int Elementos::getZsymbol(char *Symbol)
{
ElementData *q;
q=p;
string c = string(Symbol);
for (int i=0;i<MaxElem;i++){
string s = string(q->Symbol);
if (c==s) return q->AtomicNumber;
q++;
}
return 0;
}
To copy to clipboard, switch view to plain text mode
When i compile and use in debug mode works fine, but in release mode don't work.
I think that p=&Propiedades[0]; point to the first element of the Propiedades, but in getZsymbol function this pointer takes a bad value. Someone knows how can I correct this to work fine (debug and release).
Thanks
Bookmarks