0

I have a function uint8_t EE_Write(uint8_t addr, uint8_t len, uint8_t * buf) that takes a pointer to some data that it will write to memory, and a uint16_t myword that I want to give it. The basic

EE_Write(0,sizeof(myword),&myword);

gives me the compiler warning "Indirection to different types ('unsigned int *const ' instead of 'unsigned char *const ')" Even when I typecast the word (int) to a byte (char), I received the exact same warning (and no amount of grouping with parenthesis helped).

EE_Write(0,sizeof(myword),&(uint8_t)myword);

Warnings go away with a union, but it's asinine to have to write it to another variable just to make the compiler happy.

union catbyte {
    uint16_t w;
    uint8_t b[2];
};

union catbyte mybytes;

mybytes.w = myword;
EE_Write(0,sizeof(myword),mybytes.b);
0

1 Answer 1

7

You could do:

EE_Write(0,sizeof(myword),(uint8_t *)&myword);

But the prefered way of doing this would be to define the function to take a void pointer like:

uint8_t EE_Write(uint8_t addr, uint8_t len, const void * buf);

And then the compiler shouldn't warn you about it for any pointer type you pass.

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

1 Comment

Ah, should of looked at memcpy, etc. to get a clue.

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.