Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a pointer that points to two bytes

Tags:

c

pointers

i2c

I'm a complete novice in everything except maybe breathing, so sorry if I'm not being clear, but here goes:

I have a function in C which writes bytes to a circuit via an I2C bus, and in the header file it looks like this:

BOOL WINAPI JidaI2CWrite(HJIDA hJida, DWORD dwType, BYTE bAddr, LPBYTE pBytes, DWORD dwLen);
  • hJida: Board handle.
  • dwType: Zero-based number of the I2C bus.
  • bAddr: Address of the device on the I2C bus, the full 8 bits as it is written to the bus.
  • pBytes: Pointer to location that contains the bytes.
  • dwLen: Number of bytes to write.

If I wanted to write just one byte to a circuit with the address 0x98, I would do something like this:

unsigned char writing[1];
writing[0]=0x10;

unsigned char *pointer;
pointer = &writing[0];


JidaI2CWrite(hJida,0,0x98,pointer,1);

which seems to work, but if I wanted to write two bytes, say 0x10FF, it doesn't. So how do I make a pointer that points to two bytes instead of just one?

Thanks


1 Answers

You want something like this:

unsigned char writing[2];
writing[0] = 0x01;
writing[1] = 0x02;

JidaI2CWrite(hJida, 0, 0x98, writing, 2);

Notice that an array in C can be usually be used just like a pointer. The variable writing can be thought of as just a pointer to a chunk of memory that in this case has a size of 2 bytes. Creating another pointer to point to that location is redundant (in this case).

Note you could make it point to any number of bytes:

unsigned char writing[12];

//fill the array with data

JidaI2CWrite(hJida, 0, 0x98, writing, 12);
like image 182
Gabe Avatar answered Jul 02 '26 12:07

Gabe