制作一个指向两个字节的指针
除了呼吸之外,我在所有事情上都是一个完全的新手,如果我不清楚的话,很抱歉,但是这里是:
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您想要这样的东西:
请注意,C 中的数组通常可以像指针一样使用。变量
writing
可以被认为是一个指向内存块的指针,在本例中内存块的大小为 2 个字节。创建另一个指针来指向该位置是多余的(在本例中)。请注意,您可以使其指向任意数量的字节:
You want something like this:
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:
试试这个...
或者更好
注意:
sizeof(writeBuffer)/sizeof(writeBuffer[0])
会自动为您计算数组的大小(以字节为单位)Try this...
or better yet
Note:
sizeof(writeBuffer)/sizeof(writeBuffer[0])
automatically calculates the size of the array in bytes for you看起来好像
dwLen
参数是要写入的字节数。因此:请注意,您使用指向
writing[1]
的pointer
可能不应该按书面方式工作,因为这会将pointer
设置为指向您真正想要写入的字节之后的字节。我怀疑这是一个拼写错误,但如果不是,您可能希望在继续之前检查现有代码。It appears as though the
dwLen
parameter is the number of bytes to write. So:Note that your use of
pointer
pointing towriting[1]
probably shouldn't work as written, because that setspointer
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.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.我看到两种选择:
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...