Results 1 to 11 of 11

Thread: Pointers and references

  1. #1
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Pointers and references

    I've got a little problem with pointers and references.
    Here the definition of some structure that I use:

    Qt Code:
    1. struct PortConfiguration
    2. {
    3. PinConfiguration port0[8]; //!< Configuration of the port 0
    4. PinConfiguration port1[8]; //!< Configuration of the port 1
    5.  
    6. bool valid; //!< Flag: queryPinIOConfiguration() succeeds
    7. };
    8.  
    9. struct PinConfiguration
    10. {
    11. PinUserMode userIO; //!< Specify if a pin can be used by the user or if it used internaly by the module
    12. PinResistorType pullUpDownType; //!< Specify if the input pin use a pull-up or pull-down resistor
    13. PinResistorState pullUpDownState; //!< Specify if the pull-up/pull-down resistor is used or not
    14. PinDigitalIO digitalDir; //!< Specify the direction of a digital pin
    15. PinDigitalLevel outputValue; //!< Specify the digital level of a digital pin
    16. PinDigitalState digitalState; //!< Specify if a pin is a digital or analog pin
    17. };
    To copy to clipboard, switch view to plain text mode 

    This structures are defined in the class MatrixModule.
    Now, in the code of some function I want to create an array of pointers of the 2 ports: port0 and port1, so it will be easy to use in loop.

    Qt Code:
    1. void IOPortConfig::setConfig(const MatrixModule::PortConfiguration &config)
    2. {
    3. const MatrixModule::PinConfiguration* ports[2][8];
    4.  
    5. ports[0] = &(config.port0);
    6. ports[1] = &(config.port1);
    To copy to clipboard, switch view to plain text mode 

    In this way doesn't work:
    error: incompatible types in assignment of ‘const MatrixModule::PinConfiguration (*)[8]’ to ‘const MatrixModule::PinConfiguration* [8]’

    How should I do?

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pointers and references

    remove the "*" in
    Qt Code:
    1. /*const*/ MatrixModule::PinConfiguration/***/ ports[2][8];
    To copy to clipboard, switch view to plain text mode 

    EDIT:
    and also remove the "const"

  3. #3
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers and references

    Why I should remove the *? I don't want to copy all the structure, I just want a pointer.

    Anyway, I've tried your code (I also remove the const in the function), now the error is:

    error: incompatible types in assignment of ‘MatrixModule::PinConfiguration (*)[8]’ to ‘MatrixModule::PinConfiguration [8]’

    Qt Code:
    1. void IOPortConfig::setConfig(MatrixModule::PortConfiguration &config)
    2. {
    3. MatrixModule::PinConfiguration ports[2][8];
    4.  
    5. ports[0] = &(config.port0);
    6. ports[1] = &(config.port1);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pointers and references

    Quote Originally Posted by PaceyIV View Post
    Why I should remove the *? I don't want to copy all the structure, I just want a pointer.
    edit: my mistake... do it like below

    Anyway, I've tried your code (I also remove the const in the function), now the error is:
    error: incompatible types in assignment of ‘MatrixModule::PinConfiguration (*)[8]’ to ‘MatrixModule::PinConfiguration [8]’
    remove the [8]
    Qt Code:
    1. void IOPortConfig::setConfig(MatrixModule::PortConfiguration &config)
    2. {
    3. MatrixModule::PinConfiguration *ports[2]/*[8]*/;//now keep the *
    4.  
    5. ports[0] = &(config.port0);
    6. ports[1] = &(config.port1);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pointers and references

    hope now it works... i have edited the above post

  6. #6
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers and references

    I had already tried your suggestion. I had added [8] for this error message:

    error: cannot convert ‘MatrixModule::PinConfiguration (*)[8]’ to ‘MatrixModule::PinConfiguration*’ in assignment

  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pointers and references

    you have to remove the [8] and bring back the *... take a look at the post again..

  8. #8
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers and references

    Qt Code:
    1. //! Set the widget values from the PinConfiguration structure provided
    2. void IOPortConfig::setConfig(MatrixModule::PortConfiguration &config)
    3. {
    4. MatrixModule::PinConfiguration *ports[2];
    5.  
    6. ports[0] = &(config.port0);
    7. ports[1] = &(config.port1);
    To copy to clipboard, switch view to plain text mode 

    error: cannot convert ‘MatrixModule::PinConfiguration (*)[8]’ to ‘MatrixModule::PinConfiguration*’ in assignment

  9. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pointers and references

    oh.. man... i should have read your first post carefully... my mistake my mistake.. sorry.. give me a minute..

    EDIT : ... remove the & now.

    Qt Code:
    1. port[1] = /*&*/config.port;
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to nish for this useful post:

    PaceyIV (27th July 2009)

  11. #10
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pointers and references

    hope it works now

    EDIT: i was all the time confusing Port and Pin as same Configuration
    Last edited by nish; 27th July 2009 at 10:48.

  12. #11
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers and references

    It works! Good.

    Yes, sure! config.port is a pointers to config.port[0]. I should have used the & with config.port[0].
    Now I can also re-add the const.

    Thanks!

Similar Threads

  1. Use/Misuse of Pointers
    By ct in forum General Programming
    Replies: 12
    Last Post: 8th May 2007, 23:43

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.