第一个空字符终止并包括在内。 (C99 7.1.1p1)。如果一个字符序列 包含一个非结尾的空字符,那么它不是一个字符串。 因为strcat()适用于字符串,它不能用于你正在使用的序列 (因为我确定你已经发现了。 既然你没有尾随''\0''来标记 序列的结束,你必须有其他一些方法来指定它多长时间 一旦你完成了,你可以使用memcpy()(或 memmove(),如果有可能重叠)复制周围的东西。 memcpy()所需的参数由你要复制的序列的长度确定。因为你还没告诉我们这是怎么回事定义,我们不能提供更多细节。 当然,你仍然有相同的内存分配问题(制作 确定目标很大足以容纳连续的资源你有真正的琴弦。 - 基思汤普森:ks *** @ mib.org> ks *** @ mib.org < http://www.ghoti.net/~kst> 圣地亚哥超级计算机中心< * GT; < http://users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。 不要再将它们视为字符串了。他们只是数组 的字符。然后查找''memcpy()''和''memmove()''。 (观察文档中有关重叠 内存区域的符号)。 -Mike 假设: 你有多少ByteSequences ByteSequence1,ByteSequence2,... 你需要的,长度为ByteSequence1Length,ByteSequence2Length,... 并且在ByteSequence1结束时有足够的存储空间来支持其余的存储: memcpy(ByteSequence1 + ByteSequence1Length,ByteSequence2,ByteSequence2Length ); memcpy(ByteSequence1 + ByteSequence1Length + ByteSeque nce2Length, ByteSequence3,ByteSequence3Length); memcpy(ByteSequence1 + ByteSequence1Length + ByteSeque nce2Length + ByteSequence3Length , ByteSequence4,ByteSequence4Length); ... NewByteSequence1Length = ByteSequence1Length + ByteSequence2Length + ...; Gordon L. Burditt I can''t seem to find a way to concatenate strings that have nulls within thestring.I have a string that I need another string that has nulls in it and what toappendthe 2nd string, 3 string and so forth to the 1st string.Any ideas how to go about this?Thanks,jt 解决方案It''s logically impossible. A "string" is, by definition, "acontiguous sequence of characters terminated by and including thefirst null character" (C99 7.1.1p1). If a sequence of charactersincludes a null character other than at the end, it''s not a string.Since strcat() works with strings, it can''t work with the sequencesyou''re using (as I''m sure you''ve discovered.Since you don''t have the trailing ''\0'' to mark the end of yoursequence, you have to have some other way of specifying how long itis. Once you''ve done that, you can probably use memcpy() (ormemmove() if there''s a possibility of overlap) to copy things around.The required arguments to memcpy() are determined by the lengths ofthe sequences you want to copy. Since you haven''t told us how that''sdefined, we can''t provide more details.Of course, you still have the same memory allocation issues (makingsure the target is big enough to hold the concatentated result) thatyou have with real strings.--Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this.Stop thinking of them as strings. They''re simply arraysof characters. Then look up ''memcpy()'' and ''memmove()''.(Observe the notations in the documentation about overlappingareas of memory).-MikeAssuming:You have as many ByteSequences ByteSequence1, ByteSequence2, ...as you need, of length ByteSequence1Length, ByteSequence2Length, ...and that there is sufficient storage at the end of ByteSequence1 tohold the rest of them:memcpy(ByteSequence1+ByteSequence1Length, ByteSequence2, ByteSequence2Length);memcpy(ByteSequence1+ByteSequence1Length+ByteSeque nce2Length,ByteSequence3, ByteSequence3Length);memcpy(ByteSequence1+ByteSequence1Length+ByteSeque nce2Length+ByteSequence3Length,ByteSequence4, ByteSequence4Length);...NewByteSequence1Length = ByteSequence1Length + ByteSequence2Length + ... ;Gordon L. Burditt 这篇关于如何连接包含空值的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-06 01:57