Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
接收用户输入并将其存储在C中的数组中_C_Arrays_String - Fatal编程技术网

接收用户输入并将其存储在C中的数组中

接收用户输入并将其存储在C中的数组中,c,arrays,string,C,Arrays,String,我一辈子都搞不懂为什么我的代码不能产生我需要的输出。要求不使用任何功能。当我输入一行像“text”这样的文本时,生成的数组是“tex”,它将最后一个字母切掉,这对我来说毫无意义 #include <stdlib.h> #include <stdio.h> #include <ctype.h> #include <string.h> int read_input( char line_of_text[] ) { int index

我一辈子都搞不懂为什么我的代码不能产生我需要的输出。要求不使用任何功能。当我输入一行像“text”这样的文本时,生成的数组是“tex”,它将最后一个字母切掉,这对我来说毫无意义

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

int read_input( char line_of_text[] )
{
        int index = 0;
        char ch;
//      if(!line_of_text[0])
//              return index;
        for(ch = getchar(); ch != '\n' && ch != '\0'; ch = getchar()){
                if(ch == EOF){ //clear string and return EOF
                        line_of_text[0] = '\0';
                        return EOF;
                }
                line_of_text[index++] = ch;

        }
        line_of_text[++index] = '\0';
        return index;
}
#包括
#包括
#包括
#包括
int read_输入(_文本[]的字符行_)
{
int指数=0;
char ch;
//如果(!第[0]行的第[0]行)
//收益指数;
对于(ch=getchar();ch!='\n'&&ch!='\0';ch=getchar()){
如果(ch==EOF){//清除字符串并返回EOF
_文本[0]的第_行='\0';
返回EOF;
}
_text[index++]=ch的第_行;
}
_文本[++索引]='\0'的第_行;
收益指数;
}

应用所有注释并清理逻辑后

请注意,垂直和水平间距如何使代码更易于阅读/理解

注意,这些语句不使用任何“副作用”来处理“index”变量的增量

int read_input( int max_chars, char line_of_text[] )
{
    int index = 0;
    int ch = 0; // getchar() returns an int, not a char

    // Note: initialization of 'index' handled in declaration
    // Note: '-1' to leave room for NUL termination char
    for( ; index < (max_chars-1); index++ )
    {
        ch = getchar();

        // Note: place literal on left so compiler can catch  `=` error
        if( EOF == ch || '\n' == ch || '\0' == ch )
        {
            break;
        }

        // acceptable char so place into buffer
        line_of_text[index] = ch;
    }

    // when done, terminate the char string
    line_of_text[index] = '\0';

    return index;
} // end function: read_input
int read_输入(int max_chars,char line_of_text[])
{
int指数=0;
int ch=0;//getchar()返回一个int,而不是char
//注意:在声明中处理“索引”的初始化
//注:“-1”为NUL终止字符留出空间
对于(;索引<(最大字符数-1);索引++)
{
ch=getchar();
//注意:将文字放在左边,这样编译器就可以捕捉到`=`错误
如果(EOF==ch | |'\n'==ch | |'\0'==ch)
{
打破
}
//可接受的字符,因此放入缓冲区
_text[index]=ch的第_行;
}
//完成后,终止字符字符串
_文本[索引]的第_行='\0';
收益指数;
}//结束函数:读取\u输入

charch-->
int ch=0
文本[++索引]='\0'的行->
文本的第行[索引]='\0'当到达EOF时,为什么要清除字符串?我也很好奇,顺便说一句,您的代码可以正常工作。如何在实际代码中分配内存,以及如何避免缓冲区溢出?谢谢,我认为是char而不是int把事情搞砸了!!