制作一个指向两个字节的指针

发布于 2024-08-02 11:29:47 字数 669 浏览 6 评论 0原文

除了呼吸之外,我在所有事情上都是一个完全的新手,如果我不清楚的话,很抱歉,但是这里是:

我在 C 中有一个函数,它通过 I2C 总线将字节写入电路,在头文件中它看起来像这样:

BOOL WINAPI JidaI2CWrite(HJIDA hJida, DWORD dwType, BYTE bAddr, LPBYTE pBytes, DWORD dwLen);
  • hJida:板句柄。
  • dwType:I2C 从零开始的编号 公共汽车。
  • bAddr:设备的地址 I2C 总线,完整的 8 位 写入总线。
  • pBytes:指向该位置的指针 包含字节。
  • dwLen:要写入的字节数。

如果我只想向地址为 0x98 的电路写入一个字节,我会这样做:

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

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


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

这似乎可行,但如果我想写入两个字节,例如 0x10FF,则不行。那么如何制作一个指向两个字节而不是仅一个字节的指针呢?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゃ懵逼小萝莉 2024-08-09 11:29:47

您想要这样的东西:

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

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

请注意,C 中的数组通常可以像指针一样使用。变量 writing 可以被认为是一个指向内存块的指针,在本例中内存块的大小为 2 个字节。创建另一个指针来指向该位置是多余的(在本例中)。

请注意,您可以使其指向任意数量的字节:

unsigned char writing[12];

//fill the array with data

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

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);
等你爱我 2024-08-09 11:29:47

试试这个...

//A buffer containing the bytes to be written
unsigned char writeBuffer[] = {0x10, 0xFF}; 

//writeBuffer itself points to the start of the write buffer
//you dont need an extra pointer variable
//Indicate the size of the buffer in the call to the function
//pointers do not carry array size information with them (in C/C++)
JidaI2CWrite(hJida,0,0x98,writeBuffer,2); 

或者更好

unsigned char writeBuffer[] = {0x10, 0xFF};

JidaI2CWrite(hJida,0,0x98,writeBuffer
              ,sizeof(writeBuffer)/sizeof(unsigned char));

注意:sizeof(writeBuffer)/sizeof(writeBuffer[0]) 会自动为您计算数组的大小(以字节为单位)

Try this...

//A buffer containing the bytes to be written
unsigned char writeBuffer[] = {0x10, 0xFF}; 

//writeBuffer itself points to the start of the write buffer
//you dont need an extra pointer variable
//Indicate the size of the buffer in the call to the function
//pointers do not carry array size information with them (in C/C++)
JidaI2CWrite(hJida,0,0x98,writeBuffer,2); 

or better yet

unsigned char writeBuffer[] = {0x10, 0xFF};

JidaI2CWrite(hJida,0,0x98,writeBuffer
              ,sizeof(writeBuffer)/sizeof(unsigned char));

Note: sizeof(writeBuffer)/sizeof(writeBuffer[0]) automatically calculates the size of the array in bytes for you

简美 2024-08-09 11:29:47

看起来好像dwLen 参数是要写入的字节数。因此:

unsigned char writing[2];
writing[0] = 0x10;
writing[1] = 0xff;
JidaI2CWrite(hJida, 0, 0x98, writing, 2);

请注意,您使用指向 writing[1]pointer 可能不应该按书面方式工作,因为这会将 pointer 设置为指向您真正想要写入的字节之后的字节。我怀疑这是一个拼写错误,但如果不是,您可能希望在继续之前检查现有代码。

It appears as though the dwLen parameter is the number of bytes to write. So:

unsigned char writing[2];
writing[0] = 0x10;
writing[1] = 0xff;
JidaI2CWrite(hJida, 0, 0x98, writing, 2);

Note that your use of pointer pointing to writing[1] probably shouldn't work as written, because that sets pointer to point to the byte after the byte you really want to write. I'm suspecting this is a typo, but if not you may wish to review your existing code before proceeding.

黑寡妇 2024-08-09 11:29:47

writing 已经是你想要的指针了。

摆脱指针

JidaI2CWrite 的最后一个参数是要写入的字节数。

指针pBytes指向要写入的块的开头。

writing is already the pointer that you want.

Get rid of pointer.

The final parameter to JidaI2CWrite is the number of bytes you want to to write.

The pointer pBytes points to the start of the block you want to write.

黒涩兲箜 2024-08-09 11:29:47

我看到两种选择:

1)分开写:

writing[0] = 0x10;
写入[1] = 0xFF;

2)检查你的系统上的short是否为2Bytes并使用short。

可能是 ((short*)writing)[0] = 0x10FF;

另外,您需要声明写入是 char 写入[2];

然后,正如其他人所说,写入 2 个字节......

I see 2 choices:

1) write them separately:

writing[0] = 0x10;
writing[1] = 0xFF;

2) check if short on your system is 2Bytes and use a short.

probably as ((short*)writing)[0] = 0x10FF;

Also, you need to declaire writing is char writing[2];

and then, as others have said, write the 2 bytes...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文