将用户输入输入char数组(C编程)

时间:2022-08-17 03:15:46

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"

我需要从控制台读取输入并将其放入chars数组中。我编写了下面的代码,但是我得到了以下错误:“分割错误”

#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);

}

3 个解决方案

#1


8  

#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. c应该是int. getchar()返回一个int,以区分有效字符和EOF
  3. Read a character
  4. 读取一个字符
  5. Compare that character to EOF: if different jump to 7
  6. 将该字符与EOF进行比较:如果不同,跳转到7
  7. Put that character into the array arr, element count
  8. 将该字符放入数组arr,元素计数
  9. Prepare to put "another" character in the next element of the array
  10. 准备在数组的下一个元素中放入“另一个”字符
  11. Check the character read at 1. for EOF
  12. 检查字符在1处读取。对EOF

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

您需要在每次循环中读取不同的字符。(3)。4。5)。

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

你不能在数组中放入比你预留的空间更多的字符。(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.

注意,我没有使用string函数printf()。我没有使用它,因为arr不是一个字符串:它是一个简单的字符数组,并不(必然)有一个0(一个NUL)。只有带NUL的字符数组是字符串。

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中放入NUL,与其将循环限制为50个字符,不如将其限制为49个字符(为NUL保留一个空间),并在末尾添加NUL。循环后,添加

arr[count] = 0;

#2


5  

#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.

注意while循环中的&& count < 50。如果没有这个,你就可以越过arr缓冲区。

#3


3  

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

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

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

#1


8  

#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. c应该是int. getchar()返回一个int,以区分有效字符和EOF
  3. Read a character
  4. 读取一个字符
  5. Compare that character to EOF: if different jump to 7
  6. 将该字符与EOF进行比较:如果不同,跳转到7
  7. Put that character into the array arr, element count
  8. 将该字符放入数组arr,元素计数
  9. Prepare to put "another" character in the next element of the array
  10. 准备在数组的下一个元素中放入“另一个”字符
  11. Check the character read at 1. for EOF
  12. 检查字符在1处读取。对EOF

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

您需要在每次循环中读取不同的字符。(3)。4。5)。

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

你不能在数组中放入比你预留的空间更多的字符。(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.

注意,我没有使用string函数printf()。我没有使用它,因为arr不是一个字符串:它是一个简单的字符数组,并不(必然)有一个0(一个NUL)。只有带NUL的字符数组是字符串。

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中放入NUL,与其将循环限制为50个字符,不如将其限制为49个字符(为NUL保留一个空间),并在末尾添加NUL。循环后,添加

arr[count] = 0;

#2


5  

#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.

注意while循环中的&& count < 50。如果没有这个,你就可以越过arr缓冲区。

#3


3  

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

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

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