将用户输入放入字符数组(C 编程)

发布于 2024-08-04 14:27:04 字数 324 浏览 5 评论 0原文

我需要从控制台读取输入并将其放入字符数组中。我编写了以下代码,但出现以下错误:“分段错误”

#include <stdio.h>
#include <stdlib.h>

int main() {

    char c;
    int count;
    char arr[50];

    c = getchar();
    count = 0;
    while(c != EOF){
        arr[count] = c;
        ++count;
    }


    return (EXIT_SUCCESS);

}

I need to read the input from the console and put it into an array of chars. I wrote the following code, but I get the following error: "Segmentation Fault"

#include <stdio.h>
#include <stdlib.h>

int main() {

    char c;
    int count;
    char arr[50];

    c = getchar();
    count = 0;
    while(c != EOF){
        arr[count] = c;
        ++count;
    }


    return (EXIT_SUCCESS);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尤怨 2024-08-11 14:27:04
#include <stdio.h>
#include <stdlib.h>
int main() {
    char c;                /* 1. */
    int count;
    char arr[50];
    c = getchar();         /* 2. */
    count = 0;
    while (c != EOF) {     /* 3. and 6. and ... */
        arr[count] = c;    /* 4. */
        ++count;           /* 5. */
    }
    return (EXIT_SUCCESS); /* 7. */
}
  1. c 应该是一个 int。 getchar() 返回一个 int 来区分有效字符和 EOF
  2. 读取一个字符
  3. 将该字符与 EOF 进行比较:如果不同则跳转到 7
  4. 将该字符放入数组 arr,元素 count
  5. 准备将“另一个”字符放入数组的下一个元素中
  6. 检查在 1 处读取的字符。 for EOF

每次循环都需要读取不同的字符。 (3., 4., 5.)

并且您在数组中放置的字符不能多于您保留的空间。 (4.)

试试这个:

#include <stdio.h>
#include <stdlib.h>
int main() {
    int c;                 /* int */
    int count;
    char arr[50];
    c = getchar();
    count = 0;
    while ((count < 50) && (c != EOF)) {    /* don't go over the array size! */
        arr[count] = c;
        ++count;
        c = getchar();     /* get *another* character */
    }
    return (EXIT_SUCCESS);
}

编辑

在数组中拥有字符后,您会想要对它们做一些事情,对吧?因此,在程序结束之前,添加另一个循环来打印它们:

/* while (...) { ... } */
/* arr now has `count` characters, starting at arr[0] and ending at arr[count-1] */
/* let's print them ... */
/* we need a variable to know when we're at the end of the array. */
/* I'll reuse `c` now */
for (c=0; c<count; c++) {
    putchar(c);
}
putchar('\n'); /* make sure there's a newline at the end */
return EXIT_SUCCESS; /* return does not need () */

注意我没有使用字符串函数 printf()。我没有使用它,因为 arr 不是字符串:它是一个普通的字符数组,(不一定)有 0(NUL)。只有其中包含 NUL 的字符数组才是字符串。

要将 NUL 放入 arr 中,不要将循环限制为 50 个字符,而是将其限制为 49 个(为 NUL 保留一个空格)并在末尾添加 NUL。循环后添加

arr[count] = 0;
#include <stdio.h>
#include <stdlib.h>
int main() {
    char c;                /* 1. */
    int count;
    char arr[50];
    c = getchar();         /* 2. */
    count = 0;
    while (c != EOF) {     /* 3. and 6. and ... */
        arr[count] = c;    /* 4. */
        ++count;           /* 5. */
    }
    return (EXIT_SUCCESS); /* 7. */
}
  1. c should be an int. getchar() returns an int to differentiate between a valid character and EOF
  2. Read a character
  3. Compare that character to EOF: if different jump to 7
  4. Put that character into the array arr, element count
  5. Prepare to put "another" character in the next element of the array
  6. Check the character read at 1. for EOF

You need to read a different character each time through the loop. (3., 4., 5.)

And you cannot put more characters in the array than the space you reserved. (4.)

Try this:

#include <stdio.h>
#include <stdlib.h>
int main() {
    int c;                 /* int */
    int count;
    char arr[50];
    c = getchar();
    count = 0;
    while ((count < 50) && (c != EOF)) {    /* don't go over the array size! */
        arr[count] = c;
        ++count;
        c = getchar();     /* get *another* character */
    }
    return (EXIT_SUCCESS);
}

Edit

After you have the characters in the array you will want to do something to them, right? So, before the program ends, add another loop to print them:

/* while (...) { ... } */
/* arr now has `count` characters, starting at arr[0] and ending at arr[count-1] */
/* let's print them ... */
/* we need a variable to know when we're at the end of the array. */
/* I'll reuse `c` now */
for (c=0; c<count; c++) {
    putchar(c);
}
putchar('\n'); /* make sure there's a newline at the end */
return EXIT_SUCCESS; /* return does not need () */

Notice I didn't use the string function printf(). And I didn't use it, because arr is not a string: it is a plain array of characters that doesn't (necessarily) have a 0 (a NUL). Only character arrays with a NUL in them are strings.

To put a NUL in arr, instead of limiting the loop to 50 characters, limit it to 49 (save one space for the NUL) and add the NUL at the end. After the loop, add

arr[count] = 0;
围归者 2024-08-11 14:27:04
#include <stdio.h>
#include <stdlib.h>

int main() {

    int c;
    int count;
    int arr[50];

    c = getchar();
    count = 0;
    while( c != EOF && count < 50 ){
        arr[count++] = c;
        c = getchar();
    }


    return (EXIT_SUCCESS);

}

注意&&计数< while 循环中的 50。如果没有这个,你可能会溢出 arr 缓冲区。

#include <stdio.h>
#include <stdlib.h>

int main() {

    int c;
    int count;
    int arr[50];

    c = getchar();
    count = 0;
    while( c != EOF && count < 50 ){
        arr[count++] = c;
        c = getchar();
    }


    return (EXIT_SUCCESS);

}

Notice the && count < 50 in the while loop. Without this you can overrun the arr buffer.

无人问我粥可暖 2024-08-11 14:27:04

我有一个小建议。
而不是在程序中使用两次 c = getchar();
修改while循环如下,

while( (c = getchar()) != EOF && count < 50 ){
        arr[count++] = c;
}

I have a small suggestion.
Instead of having c = getchar(); twice in the program,
modify the while loop as follows,

while( (c = getchar()) != EOF && count < 50 ){
        arr[count++] = c;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文