what extra changes i need to do when i make my local variable to global variable ?
Solved
Mobile and Embedded
-
typedef struct
{
int nI2C;
unsigned char nAddress;
}TI2C;
TI2C i2c; void *pHandle = (void*) & i2c; qDebug("gauge test\n\r"); if((i2c.nI2C= open("/dev/i2c-2", O_RDWR))<0) { qDebug("can not open I2C bus \n\r"); return bStatus; } qDebug("openend I2C bus\n\r"); unsigned char nAddress=GAUGE_DEVICE_ADDRESS; TI2C *pI2C= (TI2C*) pHandle; if(nAddress!= pI2C->nAddress) ioctl(pI2C->nI2C,I2C_SLAVE,nAddress>>1); else { qDebug("Battery fuel gauge not found\n"); return bStatus; } bStatus= true; pI2C->nAddress= nAddress; return bStatus;
i need below questions answer regarding above code.
1] what extra changes i need to do to make pI2C to make global structure pointer so that application not going to crash ?
2] what extra changes i need to do to make pHandle to make global structure pointer so that application not going to crash ?
-
@Qt-embedded-developer Basics:
- Make sure it is initialised (a pointer to valid instance assigned) before you use it
- Same here
You should rething your design as global variables are usually a sign for bad design...
-
@Qt-embedded-developer You should think about where these variables are really needed and keep them there (for example in a class which manages the access to them).