C语言 如何在C中填充字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33689274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fill a char array in C
提问by George I.
Starting from
从...开始
char tval[20] = "temp:26.62";
how can I add a space character until tvalis filled? I need that in the end to obtain this:
如何添加空格字符直到tval被填满?我最终需要它来获得这个:
char tval[20] = "temp:26.62 ";
回答by V. Kravchenko
Like this, probably
像这样,大概
size_t prevlen = strlen(tval);
memset(tval + prevlen, ' ', 19 - prevlen);
*(tval + 19) = 'char tval[20] = "temp:26.62";
';
回答by Magisch
If tval[20]is a fixed size and that 20is always gonna be the size, you can iterate through the array and do this:
如果tval[20]是固定大小并且20始终是大小,则可以遍历数组并执行以下操作:
{'t','e','m','p',':','2','6','.','6','2','for (int i = 0; i < 19; i++)
{
if (tval[i] == 'char tval [] = "Hello";
')
{
tval[i] = ' ';
}
}
'}
What happens now is that your char tval[20]looks like this:
现在发生的是你的字符tval[20]看起来像这样:
#include<stdio.h>
#include<string.h>
int main(void){
char tval[20] = "temp:26.62";
size_t length = strlen(tval);
printf("Length of TVAL Before = %zu\n", length);
while(length < 19){
tval[length] = ' ';
length++;
}
tval[19] = 'Length of TVAL Before = 10
Length of TVAL After = 19
TVAL = temp:26.62
';
printf("Length of TVAL After = %zu\n",length);
printf("TVAL = %s\n",tval);
return 0;
}
The rest will be filled with \0
其余的将充满 \0
You can now iterate:
您现在可以迭代:
Length of TVAL Before = 10
Length of TVAL After = 19
TVAL = temp:26.62
It is important that you terminate the string with a nullbyte. If you choose not to, you can not use any string functions on the char array because you will be invoking undefined behavior if you do so.
使用空字节终止字符串很重要。如果您选择不这样做,则不能在 char 数组上使用任何字符串函数,因为这样做将调用未定义的行为。
回答by Eric Mbatchou
In general, the tables are initialized by the indication of the list of array elements in braces:
通常,表是通过大括号中数组元素列表的指示来初始化的:
??char tval [] = {'H', 'e', ??'l', 'l', 'o', '\ 0'};
??char tval [] = {'H', 'e', ??'l', 'l', 'o', '\ 0'};
For the special case character arrays, we can use a more comfortable boot simply stating a constant string:
对于特殊情况的字符数组,我们可以使用更舒适的引导,只需声明一个常量字符串:
#include<stdio.h>
#include<string.h>
int main(void){
char tval[20] = "temp:26.62";
size_t length = strlen(tval);
printf("Length of TVAL Before = %zu\n\n", length);
while(length < 19){
tval[length] = ' ';
length++;
}
tval[18] = '.';
tval[19] = 'Length of TVAL Before = 10
TVAL = temp:26.62 .
Length of TVAL After = 19
';
printf("TVAL = %s\n\n",tval);
printf("Length of TVAL After = %zu\n",length);
return 0;
}
During initialization by [], the computer automatically reserves the number of bytes required for the chain, ie .: the number of character + 1 (here: 6 bytes). We can also explicitly specify the number of bytes to be reserved, if it is greater or equal to the length of the initialization string.
用[]初始化时,计算机自动预留链所需的字节数,即.:字符数+1(这里:6字节)。我们还可以明确指定要保留的字节数,如果它大于或等于初始化字符串的长度。
回答by Michi
Try this:
尝试这个:
Length of TVAL Before = 10
TVAL = temp:26.62 .
Length of TVAL After = 19
Output:
输出:
##代码##
##代码##
You can use a dot to check it:
您可以使用点来检查它:
##代码##Output:
输出:
##代码##
##代码##

