作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图从用户那里获取输入(字符串)并将它们存储在一个数组中。但是在我运行这段代码后,程序立即崩溃了。
#include <stdio.h>
int main() {
int i;
char *word[3];
for(i=0;i<3;i++)
{
printf(" Enter a word: ");
scanf("%s", &word[i]);
}
printf("%s ", word[0]);
return 0;
}
最佳答案
在这一行:
scanf("%s", &word[i]);
word[i]
指向某处,并且有足够的空间来占据输入的字符串。自
word[i]
是
char *
指针,您需要在某个时候为此分配内存。否则,它只是一个不指向任何地方的悬空指针。
scanf()
,那么你可以预先用
malloc
分配一些空间.
malloc()
allocates requested memory on the heap, then returns avoid*
pointer at the end.
malloc()
在你的代码中是这样的:
size_t malloc_size = 100;
for (i = 0; i < 3; i++) {
word[i] = malloc(malloc_size * sizeof(char)); /* allocates 100 bytes */
printf("Enter word: ");
scanf("%99s", word[i]); /* Use %99s to avoid overflow */
/* No need to include & address, since word[i] is already a char* pointer */
}
malloc()
的返回值,因为它可以返回
NULL
不成功的时候。
malloc()
分配内存时,您必须使用
free
最后释放请求的内存:
free(word[i]);
word[i] = NULL; /* safe to make sure pointer is no longer pointing anywhere */
fgets
.
char *fgets(char *str, int n, FILE *stream)
reads a line from an input stream, and copies the bytes over tochar *str
, which must be given a size ofn
bytes as a threshold of space it can occupy.
\n
缓冲区末尾的字符。可以轻松移除。 NULL
.如果没有读取字符,仍然返回 NULL
在末尾。 n
. stdin
或 FILE *
. stdin
的输入行的示例。 :
char buffer[100]; /* statically declared buffer */
printf("Enter a string: ");
fgets(buffer, 100, stdin); /* read line of input into buffer. Needs error checking */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMSTR 3
#define BUFFSIZE 100
int main(void) {
char *words[NUMSTR];
char buffer[BUFFSIZE];
size_t i, count = 0, slen; /* can replace size_t with int if you prefer */
/* loops only for three input strings */
for (i = 0; i < NUMSTR; i++) {
/* read input of one string, with error checking */
printf("Enter a word: ");
if (fgets(buffer, BUFFSIZE, stdin) == NULL) {
fprintf(stderr, "Error reading string into buffer.\n");
exit(EXIT_FAILURE);
}
/* removing newline from buffer, along with checking for overflow from buffer */
slen = strlen(buffer);
if (slen > 0) {
if (buffer[slen-1] == '\n') {
buffer[slen-1] = '\0';
} else {
printf("Exceeded buffer length of %d.\n", BUFFSIZE);
exit(EXIT_FAILURE);
}
}
/* checking if nothing was entered */
if (!*buffer) {
printf("No string entered.\n");
exit(EXIT_FAILURE);
}
/* allocate space for `words[i]` and null terminator */
words[count] = malloc(strlen(buffer)+1);
/* checking return of malloc, very good to do this */
if (!words[count]) {
printf("Cannot allocate memory for string.\n");
exit(EXIT_FAILURE);
}
/* if everything is fine, copy over into your array of pointers */
strcpy(words[count], buffer);
/* increment count, ready for next space in array */
count++;
}
/* reading input is finished, now time to print and free the strings */
printf("\nYour strings:\n");
for (i = 0; i < count; i++) {
printf("words[%zu] = %s\n", i, words[i]);
free(words[i]);
words[i] = NULL;
}
return 0;
}
Enter a word: Hello
Enter a word: World
Enter a word: Woohoo
Your strings:
words[0] = Hello
words[1] = World
words[2] = Woohoo
关于c - 如何将字符串输入到C中的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518039/
我是一名优秀的程序员,十分优秀!