简体   繁体   English

C编程:如何填充数组?

[英]C programming: how to fill an array?

I know strncpy(s1, s2, n) copies n elements of s2 into s1, but that only fills it from the beginning of s1. 我知道strncpy(s1,s2,n)将s2的n个元素复制到s1中,但这只是从s1的开头填充它。 For example 例如

s1[10] = "Teacher"
s2[20] = "Assisstant"
strncpy(s1, s2, 2) would yield s1[10] = "As", correct?

Now what if I want s1 to contain "TeacherAs"? 现在如果我想让s1包含“TeacherAs”怎么办? How would I do that? 我该怎么办? Is strncpy the appropriate thing to use in this case? strncpy在这种情况下适用吗?

You can use strcat() to concatenate strings, however you don't want all of the source string copied in this case, so you need to use something like: 您可以使用strcat()来连接字符串,但是您不希望在这种情况下复制所有源字符串,因此您需要使用以下内容:

size_t len = strlen(s1);
strncpy(s1 + len - 1, s2, 2);
s2[len + 2] = '\0';

(Add terminating nul; thanks @FatalError). (添加终止nul;感谢@FatalError)。

Which is pretty horrible and you need to worry about the amount of space remaining in the destination array. 这非常可怕,您需要担心目标阵列中剩余的空间量。 Please note that if s1 is empty that code will break! 请注意,如果s1为空,则代码将中断!

There is strncat() ( manpage ) under some systems, which is much simpler to use: 在某些系统下有strncat()联机帮助页 ),使用起来要简单得多:

strncat(s1, s2, 2);

Use strcat . 使用strcat

Make sure your string you're appending to is big enough to hold both strings. 确保你附加的字符串大到足以容纳两个字符串。 In your case it isn't. 在你的情况下,它不是。

From the link above: 从上面的链接:
char * strcat ( char * destination, const char * source ); char * strcat(char * destination,const char * source);

Concatenate strings Appends a copy of the source string to the destination string. 连接字符串将源字符串的副本附加到目标字符串。 The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination. 目标中的终止空字符被源的第一个字符覆盖,并且在由目标中的两个串联形成的新字符串的末尾包括空字符。

destination and source shall not overlap. 目的地和来源不得重叠。

In order to achieve what you need you have to use strlcat (but beware! it is considered insecure ) 为了达到你所需要的,你必须使用strlcat (但要小心!它被认为是不安全的

strlcat(s1, s2, sizeof(s1));

This will concatenate to s1 , part of the s2 string, until the size of s1 is reached (this avoids memory overflow ) 这将连接到s1s2字符串的一部分,直到达到s1的大小(这避免了memory overflow

then you'll get into s1 the string TeacherAs + a NUL char to terminate it 那么你将进入s1字符串TeacherAs +一个NUL字符来终止它

you need to make sure that you have enough memory is allocated for the resulting string 您需要确保为结果字符串分配了足够的内存

s1[10]

is not enough space to fit 'TeacherAs'. 没有足够的空间来适应'TeacherAs'。

from there, you'll want to do something like 从那里,你会想要做类似的事情

//make sure s1 is big enough to hold s1+s2
s1[40]="Teacher";
s2[20]="Assistant";

//how many chars from second string you want to append
int offset = 2;
//allocate a temp buffer 
char subbuff[20];
//copy n chars to buffer
memcpy( subbuff, s2, offset );
//null terminate buff
subbuff[offset+1]='\0';

//do the actual cat
strcat(s1,subbuff);

I'd suggest using snprintf() , like: 我建议使用snprintf() ,如:

size_t len = strlen(s1);
snprintf(s1 + len, sizeof(s1) - len, "%.2s", s2);

snprintf() will always nul terminate and won't overrun your buffer. snprintf()将始终终止并且不会超出您的缓冲区。 Plus, it's standard as of C99. 另外,它是C99的标准配置。 As a note, this assumes that s1 is an array declared in the current scope so that sizeof works, otherwise you'll need to provide the size. 请注意,这假设s1是在当前作用域中声明的数组,因此sizeof可以工作,否则您需要提供大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM