如何寻址(一维)数组的一部分?
我是 C 编程的新用户。我尝试过在线研究这个问题,但找不到答案...如何访问 C 中数组的一部分?例如,
int Data[4]
int Input[32]
执行的语法是什么: Data = Input[12:15] 这样
Data[0] = Input[12]
Data[1] = Input[13]
Data[2] = Input[14]
Data[3] = Input[15]
实际上我正在尝试使用 TCP 套接字填充数组的一部分:
recv(MySocket, YRaw[indx:indx+1024], sizeChunk, 0)
我希望将接收到的数据放置在 YRaw 数组中数组索引“indx”到“indx+1024”。
预先感谢,gkk
I'm a new user to C programming. I've tried researching this online, but couldn't find an answer... how to I access a portion of an array in C? For example,
int Data[4]
int Input[32]
What's the syntax for doing: Data = Input[12:15] such that
Data[0] = Input[12]
Data[1] = Input[13]
Data[2] = Input[14]
Data[3] = Input[15]
In reality I'm trying to fill a portion of an array using a TCP socket:
recv(MySocket, YRaw[indx:indx+1024], sizeChunk, 0)
where I want the received data to be placed in YRaw array from array index 'indx' to 'indx+1024'.
Thanks in advance, gkk
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要将内容从一个数组复制到另一个数组,您可以使用memcpy:
在recv的情况下,您执行相同的操作 - 传入指向开头的指针和字节数:
编辑: 我忘记了第二个示例中的 sizeof,所以我添加了它。
For copying things from one array to another, you could use
memcpy
:In the case of recv, you do the same thing - you pass in the pointer to the start and the number of bytes:
Edit: I forgot sizeof from the second example so I added it.
可以使用memcpy
could use memcpy
您可以使用指针算术:
在这种情况下,recv 会将第一个 int 放置在 YRaw[indx] 处,第二个 int 放置在 YRaw[indx + 1] 处,依此类推。
在此示例中,我假设您想从套接字读取整数。
另外,不要忘记检查返回值。
You could use pointer-arithmetics:
In this case, recv will place the first int at YRaw[indx], the second at YRaw[indx + 1], and so on.
In this example I assumed that you'd like to read integers from the socket.
Also, don't forget to check the return value.