C语言 C编程打印字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7865654/
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
C programming printing array of characters
提问by ilovetolearn
I have an array of characters declared as:
我有一个字符数组声明为:
char *array[size];
When I perform a
当我执行一个
printf("%s", array);
it gives me some garbage characters, why it is so?
它给了我一些垃圾字符,为什么会这样?
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
This url indicates printf takes in the format of: `int printf ( const char * format, ... );
此 url 表示 printf 采用以下格式:`int printf ( const char * format, ... );
#include <stdio.h>
#include <string.h>
#define size 20
#define buff 100
char line[buff];
int main ()
{
char *array[100];
char *sep = " \t\n";
fgets(line, buff, stdin);
int i;
array[0] = strtok(line, sep);
for (i = 1; i < size; i++) {
array[i] = strtok(NULL, sep);
if (array[i] == NULL)
break;
}
return 0;
}
采纳答案by Tony The Lion
Your array is not initialized, and also you have an array of pointers, instead of an array of char's. It should be char* array = (char*)malloc(sizeof(char)*size);, if you want an array of char's. Now you have a pointer to the first element of the array.
你的数组没有初始化,而且你有一个指针数组,而不是一个字符数组。char* array = (char*)malloc(sizeof(char)*size);如果你想要一个字符数组,它应该是。现在你有一个指向数组第一个元素的指针。
回答by Sebastian Paaske T?rholm
You declare an array of characters like so:
您可以像这样声明一个字符数组:
char foo[size];
You seem to have it mixed up with char *, which is a pointer to a character. You could say
您似乎将它与char *,这是一个指向字符的指针混合在一起。你可以说
char *bar = foo;
which would make barpoint to the contents of foo. (Or, actually, to the first character of foo.)
这将bar指向foo. (或者,实际上,到 . 的第一个字符foo。)
To then print the contents of the array, you can do one of the following:
要打印数组的内容,您可以执行以下操作之一:
// either print directly from foo:
printf("%s", foo);
// or print through bar:
printf("%s", bar);
Note, however, that C performs no initialization of the contents of variables, so unless you specifically set the contents to something, you'll get garbage. In addition, if that garbage doesn't happen to contain a \0; that is, a char with value 0, it will keep on outputting past the end of the array.
但是请注意,C 不执行变量内容的初始化,因此除非您专门将内容设置为某些内容,否则您会得到垃圾。此外,如果该垃圾不碰巧包含\0; 也就是说,一个值为 0 的字符,它将继续输出超过数组的末尾。
回答by AusCBloke
Why are we making such a simple thing sound so difficult?
为什么我们让这么简单的事情听起来这么难?
char array[SIZE];
... /* initialize array */
puts(array); /* prints the string/char array and a new line */
/* OR */
printf("%s", array); /* prints the string as is, without a new line */
The charin arrayafter the end of what you want to be your string (ie. if you want your string to read "Hello" that would be the next char after the 'o') must be the terminating NUL character '\0'. If you use a C function to read input that would automatically be appended to the end of your buffer. You would only need to worry about doing it manually if you were individually writing characters to your buffer or something for some reason.
该字符的数组,你想成为你的字符串是什么结束后(即,如果你想你的字符串为“你好”,这将是之后的下一个字符的“o”)必须终止NULL字符“\ 0” . 如果您使用 C 函数读取将自动附加到缓冲区末尾的输入。如果您出于某种原因将字符单独写入缓冲区或其他内容,则只需担心手动执行此操作。
EDIT: As with pmg's comment, the '\0' goes wherever you want the stringto end, so if you wanted to shorten your string you could just move it up closer to the front, or to have an empty stringyou just have array[0] = '\0';. Doing so can also be used to tokenise smaller strings inside a single buffer, just as strtokdoes. ie. "Part1\0Part2\0Part3\0". But I think this is getting away from the scope of the question.
编辑:与 pmg 的评论一样, '\0' 可以放在你想要字符串结束的任何地方,所以如果你想缩短你的字符串,你可以把它移到更靠近前面的地方,或者你有一个空字符串array[0] = '\0';。这样做也可以用于在单个缓冲区内标记较小的字符串,就像strtok一样。IE。“Part1\0Part2\0Part3\0”。但我认为这已经脱离了问题的范围。
ie. you wanted to store the first 3 chars of the alphabet as a string (don't know why anyone would do it this way but it's just an example):
IE。您想将字母表的前 3 个字符存储为字符串(不知道为什么有人会这样做,但这只是一个示例):
char array[4];
array[0] = 'a';
array[1] = 'b';
array[2] = 'c';
array[3] = 'char *array[size];
';
printf("%s\n", array);
If you have something like char array[] = "Hello";the '\0' is automatically added for you.
如果你有类似char array[] = "Hello";'\0' 的东西,它会自动为你添加。
回答by Mat
char array[size];
arrayis not a char *with that, it's more like a char **(pointer to an array of chars, with is similar to pointer to pointer to char).
array不是一个char *,它更像是一个char **(指向字符数组的指针,与指向指向字符的指针的指针类似)。
If all you need is a C string, either:
如果您只需要一个 C 字符串,则可以:
char *array;
and make sure you 0-terminate it properly, or
并确保您正确地 0 终止它,或者
##代码##and make sure you properly allocate and free storage for it (and 0-terminate it too).
并确保为它正确分配和释放存储空间(并且也以 0 终止它)。

