C - 子字符串(从 POS 到 POS)
我有一个长度为 32 的字符数组,想从中取出某些字符。 例如
1111110000000000000000000111111
<32个字符
我想采用字符0-6,这将是111111
或者甚至采用字符26-31,这将是111111
code>
char check_type[32];
以上是我的声明方式。
我希望能够做的是定义一个函数或使用一个占据起始位置和结束字符的函数。
我已经研究了很多方法,例如使用 strncpy
和 strcpy
但还没有找到方法。
I have a character array of length 32 and would like to take certain charcters out of it.
for example
111111000000000000000000111111
<32 chars
I would like to take chars 0-6 which would be 111111
Or even take chars 26-31 which would be 111111
char check_type[32];
Above is how I'm declaring.
What I would like to be able to do is define a function or use a function that takes that starting place, and end character.
Ive looked at many ways like using strncpy
and strcpy
but found no way yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会简单地包装
strncpy
:输出:
I would simply wrap
strncpy
:Output:
使用memcpy。
Use
memcpy
.示例:
当然,当您不再需要返回代码时,请释放它。您会注意到此函数不会检查
endpos
与startpos
的有效性。Sample:
Of course, free the return code when you don't need it anymore. And you notice this function will not check for the validity of
endpos
vsstartpos
.首先定义所需的接口...也许:
这需要一个将复制数据的目标(目标)数组,并给出其长度。数据将来自源数组,位置
src_bgn
和src_end
之间。如果出现错误,则返回值为 -1,并且返回输出的长度(不包括终止 null)。如果目标字符串太短,则会出现错误。有了这组细节,您就可以相当轻松地实现主体,并且
strncpy()
这次可能很合适(通常不合适)。用法(根据您的问题):
First define the required interface...perhaps:
This takes a destination (target) array where the data will be copied, and is given its length. The data will come from the source array, between positions
src_bgn
andsrc_end
. The return value will be -1 for an error, and the length of the output (excluding the terminating null). If the target string is too short, you will get an error.With that set of details in place, you can implement the body fairly easily, and
strncpy()
might well be appropriate this time (it often isn't).Usage (based on your question):
检查一下:
Check this: