PDA

View Full Version : Pointers and references



PaceyIV
27th July 2009, 09:45
I've got a little problem with pointers and references.
Here the definition of some structure that I use:



struct PortConfiguration
{
PinConfiguration port0[8]; //!< Configuration of the port 0
PinConfiguration port1[8]; //!< Configuration of the port 1

bool valid; //!< Flag: queryPinIOConfiguration() succeeds
};

struct PinConfiguration
{
PinUserMode userIO; //!< Specify if a pin can be used by the user or if it used internaly by the module
PinResistorType pullUpDownType; //!< Specify if the input pin use a pull-up or pull-down resistor
PinResistorState pullUpDownState; //!< Specify if the pull-up/pull-down resistor is used or not
PinDigitalIO digitalDir; //!< Specify the direction of a digital pin
PinDigitalLevel outputValue; //!< Specify the digital level of a digital pin
PinDigitalState digitalState; //!< Specify if a pin is a digital or analog pin
};


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.



void IOPortConfig::setConfig(const MatrixModule::PortConfiguration &config)
{
const MatrixModule::PinConfiguration* ports[2][8];

ports[0] = &(config.port0);
ports[1] = &(config.port1);


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?

nish
27th July 2009, 09:50
remove the "*" in

/*const*/ MatrixModule::PinConfiguration/***/ ports[2][8];

EDIT:
and also remove the "const"

PaceyIV
27th July 2009, 10:12
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]’



void IOPortConfig::setConfig(MatrixModule::PortConfigur ation &config)
{
MatrixModule::PinConfiguration ports[2][8];

ports[0] = &(config.port0);
ports[1] = &(config.port1);

nish
27th July 2009, 10:19
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]


void IOPortConfig::setConfig(MatrixModule::PortConfigur ation &config)
{
MatrixModule::PinConfiguration *ports[2]/*[8]*/;//now keep the *

ports[0] = &(config.port0);
ports[1] = &(config.port1);

nish
27th July 2009, 10:23
hope now it works... i have edited the above post :)

PaceyIV
27th July 2009, 10:26
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

nish
27th July 2009, 10:29
you have to remove the [8] and bring back the *... take a look at the post again..

PaceyIV
27th July 2009, 10:33
//! Set the widget values from the PinConfiguration structure provided
void IOPortConfig::setConfig(MatrixModule::PortConfigur ation &config)
{
MatrixModule::PinConfiguration *ports[2];

ports[0] = &(config.port0);
ports[1] = &(config.port1);


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

nish
27th July 2009, 10:39
oh.. man... i should have read your first post carefully... my mistake my mistake.. sorry.. give me a minute..

EDIT : ... remove the & now.


port[1] = /*&*/config.port;

nish
27th July 2009, 10:42
hope it works now :)

EDIT: i was all the time confusing Port and Pin as same Configuration :)

PaceyIV
27th July 2009, 10:53
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!