1

I don't know why the below code give invalid Initializer error. Is there something wrong?

void ssd_write(uint8_t data){

        uint8_t txBuffer[1] = data;    <--- error

        i2cTransaction.slaveAddress = 0x78;
        i2cTransaction.writeBuf   = txBuffer;
        i2cTransaction.writeCount = 1;
        i2cTransaction.readBuf    = NULL;
        i2cTransaction.readCount  = 0;
        I2C_transfer(i2c, &i2cTransaction);
        I2C_close(i2c);

    }

1 Answer 1

6

It should be

uint8_t txBuffer[1] = {data};

Yoou are declaring an array of 1 element and initializing it. You should be aware that in this case you are assigning to writeBuf the decayed pointer pointing to the array's first element.

Otherwise you are simply doing an invalid operation by trying to assign the variable to the declared array.

Or more simply you might wanted (Because it is illogical to have an array to store just a single value - you can but that's hardly the thing we do)

uint8_t txBuffer = data;

After you do this, you might want to do i2cTransaction.writeBuf = &txBuffer; if it expects a uint8_t*.

Sign up to request clarification or add additional context in comments.

3 Comments

... and in the scalar approach, one then assigns i2cTransaction.writeBuf = &txBuffer (i.e. one assigns the address of txBuffer instead of assigning txBuffer itself).
@JohnBollinger.: Edited. The difference between two and all
@BenjaminBarrois.: Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.