Results 1 to 4 of 4

Thread: Structure and pointers

  1. #1
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Structure and pointers

    I need to store a list with chemical elements and its properties. I have programmed a class like this:
    Qt Code:
    1. class Elementos
    2. {
    3. public:
    4. Elementos();
    5. ~Elementos();
    6. int getZsymbol(char *Symbol);
    7. .....
    8. private:
    9. enum {MaxElem=118,NameLen=16,NN=-999};
    10. struct ElementData
    11. {
    12. int AtomicNumber;
    13. char Symbol[3+1];
    14. char SpanishName[NameLen+1];
    15. char EnglishName[NameLen+1];
    16. char GermanName[NameLen+1];
    17. .....
    18. }Propiedades[MaxElem],*p;
    19. }
    To copy to clipboard, switch view to plain text mode 

    And in cpp file:

    Qt Code:
    1. #include "elementos.h"
    2.  
    3. Elementos::Elementos()
    4. {
    5. ElementData Propiedades[MaxElem] =
    6. {
    7. { 1, "H" , "Hidrógeno" , "Hydrogen" , "Wasserstoff" },
    8. { 2, "He", "Helio" , "Helium" , "Helium"}
    9. ...
    10. };
    11. p=&Propiedades[0];
    12. }
    13.  
    14. int Elementos::getZsymbol(char *Symbol)
    15. {
    16. ElementData *q;
    17. q=p;
    18. string c = string(Symbol);
    19. for (int i=0;i<MaxElem;i++){
    20. string s = string(q->Symbol);
    21. if (c==s) return q->AtomicNumber;
    22. q++;
    23. }
    24. return 0;
    25. }
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Structure and pointers

    Quote Originally Posted by zorro68 View Post
    ElementData Propiedades[MaxElem] =
    {
    { 1, "H" , "Hidrógeno" , "Hydrogen" , "Wasserstoff" },
    { 2, "He", "Helio" , "Helium" , "Helium"}
    ...
    };
    You define a new Propiedades variable here, so not only you don't initialize Elementos::Propiedades, but also you assign an address of a temporary object to p.

    Do you reall want to store a copy of that table in every Elementos instance? I would define Propiedades as static.

  3. #3
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Structure and pointers

    You define a new Propiedades variable here, so not only you don't initialize Elementos::Propiedades, but also you assign an address of a temporary object to p.
    I have proved this:

    Qt Code:
    1. ....
    2. }*Propiedades[MaxElem],*p;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Propiedades[MaxElem]=new ElementData;
    2. Propiedades[MaxElem] =
    3. {
    4. { 1, "H" , "Hidrógeno" , "Hydrogen" , "Wasserstoff"},
    5. { 2, "He", "Helio" , "Helium" , "Helium"}
    6. ....
    To copy to clipboard, switch view to plain text mode 

    and i get a lot of syntax errors and i don't know why. Or this:
    Qt Code:
    1. Propiedades[0]={ 1, "H" , "Hidrógeno" , "Hydrogen" , "Wasserstoff"};
    2. Propiedades[1]={ 2, "He", "Helio" , "Helium" , "Helium"};
    To copy to clipboard, switch view to plain text mode 

    I get the same error.

    Do you reall want to store a copy of that table in every Elementos instance?
    No, but i don't know how?

    I would define Propiedades as static.
    How?
    Last edited by zorro68; 22nd October 2007 at 14:20.

  4. #4
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Structure and pointers

    I used an static variable Propiedades out of the class definition and works fine.

    Thanks very much.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.