Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Input_Scanf_C Strings - Fatal编程技术网

C 我怎么知道行号什么时候变了?

C 我怎么知道行号什么时候变了?,c,string,input,scanf,c-strings,C,String,Input,Scanf,C Strings,我知道我可以将单词(作为字符串)存储在具有足够空间的字符数组中 在特定示例中,我有一个3个字符串的数组,每个字符串有5个字节。 这是我从一行字中提取单词的方式: int main(void) { int i; char **array; array = (char **)malloc(3 * sizeof(char *)); // allocation for( i = 0; i <= 2; i++ ) { array[i] = (

我知道我可以将单词(作为字符串)存储在具有足够空间的字符数组中

在特定示例中,我有一个3个字符串的数组,每个字符串有5个字节。
这是我从一行字中提取单词的方式:

int main(void)
{
    int i;
    char **array;
    array = (char **)malloc(3 * sizeof(char *)); // allocation

    for( i = 0; i <= 2; i++ )
    {
        array[i] = (char *)malloc(5 * sizeof(char)); // allocation
    }

    /* now I store the words */
    i = 0;
    while(i <= 2)
    {
        scanf("%s", array[i]);
        i++;
    }
}
int main(无效)
{
int i;
字符**数组;
数组=(char**)malloc(3*sizeof(char*);//分配
对于(i=0;i
#包括
#包括
#包括
#定义SIZEOF_数组(x)(SIZEOF(x)/SIZEOF((*x)))
int peek(文件*流)
{
返回ungetc(fgetc(流),流);
}
int main()
{
字符字[3][5]={0};
字数=0;
对于(size_t i=0;i
大家好,我找到了一个解决方案。在我得到一个字符串之后,我将getchar()放在一边,看看它是否是“\n.btw thnx

使用
scanf(“%s”,array[i]);
-->
scanf(“%4s”,array[i]);
防止缓冲区溢出。“要知道行何时发生了变化”
scanf array[i])
不跟踪行,因为
'\n'
是先消耗的,没有统计。使用
fgets()
读取一行用户输入。代码需要一种新的方法。在编译时知道数组的尺寸时,为什么对这样小的数组使用
malloc()