【问题标题】:Putting user input into char array (C Programming)将用户输入放入 char 数组(C 编程)
【发布时间】:2010-11-27 07:20:59
【问题描述】:

我需要从控制台读取输入并将其放入一个字符数组中。我编写了以下代码,但出现以下错误:“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);

}

【问题讨论】:

  • 如果我想在循环中打印当前字符并添加以下内容: printf(arr[count]);我再次遇到分段错误。

标签: c arrays segmentation-fault


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    #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循环中的&& count 。如果没有这个,您可能会超出 arr 缓冲区。

    【讨论】:

    • 哈哈,这很简单。抱歉,我是 C 新手,这是我的第一个程序。谢谢。
    • 如果没有“c = getchar();”,这仍然无法工作循环中的某处。
    【解决方案3】:
    #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 应该是一个整数。 getchar() 返回一个 int 来区分有效字符和 EOF
    2. 读取一个字符
    3. 将该字符与 EOF 进行比较:如果不同则跳转到 7
    4. 将该字符放入数组arr,元素count
    5. 准备将“另一个”字符放入数组的下一个元素中
    6. 检查在 1. 处读取的字符是否为 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;
    

    【讨论】:

    • 为了它的价值,与其在循环中使用putchar(),不如只使用fwrite(arr, 1, count, stdout);
    • 有没有办法动态分配数组的大小? (如果输入数量未知)
    • @Ir0nm:是的。您可以在循环内使用realloc() 来根据需要继续增长数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多