Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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
为什么getchar()在2d数组中存储重复值?_C - Fatal编程技术网

为什么getchar()在2d数组中存储重复值?

为什么getchar()在2d数组中存储重复值?,c,C,所以基本上我试图在每一行的第6个位置存储一个字符,我已经成功地使用了它,但是我得到了相同的值,在期望的位置和它的后续位置存储了两次 我不知道出了什么问题 我尝试过使用while((getchar())!='\n') 使用calloc而不是malloc。这样内存将被初始化为零 请不要忘记将分配大小(即6)和索引计算更新为相同的数字。您已分配6,但使用了&p[i*5+5] 粘贴了两个更改 char *p=(char*)calloc(limit*6, sizeof(char)); scanf("%

所以基本上我试图在每一行的第6个位置存储一个字符,我已经成功地使用了它,但是我得到了相同的值,在期望的位置和它的后续位置存储了两次

我不知道出了什么问题


  • 我尝试过使用while((getchar())!='\n') 使用calloc而不是malloc。这样内存将被初始化为零

    请不要忘记将分配大小(即6)和索引计算更新为相同的数字。您已分配6,但使用了&p[i*5+5]

    粘贴了两个更改

    char *p=(char*)calloc(limit*6, sizeof(char)); 
    scanf("%c",&p[i*6+5]);  
    

    &p[i*5+5]
    在分配之外。它仍然在存储重复的值请记住
    malloc
    并没有将内存设置为零。使用
    calloc
    。为什么同时使用
    getchar
    scanf
    ?欢迎使用堆栈溢出!您的代码不完整;特别是,它似乎缺少一个
    main()
    函数和至少一个
    #include
    。请输入您的代码,使其成为您问题的一部分(包括任何必要的输入,但最好不需要任何输入),然后我们可以尝试复制并解决它。您还应该阅读。那么我不必使用getchar()?我已经在使用calloc而不是malloc时发布了输出
    Value at row 0 is = 
    Value at row 0 is = 
    Value at row 0 is = 
    Value at row 0 is = 
    Value at row 0 is = 
    Value at row 0 is = 1
    Value at row 1 is = 1
    Value at row 1 is = 
    Value at row 1 is = 
    Value at row 1 is = 
    Value at row 1 is = 
    Value at row 1 is = 2
    Value at row 2 is = 2
    Value at row 2 is = 
    Value at row 2 is = 
    Value at row 2 is = 
    Value at row 2 is = 
    Value at row 2 is = 3
    Input Verified
    
    
    Value at row 0 is = 0
    Value at row 0 is = 
    Value at row 0 is = 
    Value at row 0 is = 
    Value at row 0 is = 
    Value at row 0 is = 1
    Value at row 1 is = 
    Value at row 1 is = 0
    Value at row 1 is = 
    Value at row 1 is = 
    Value at row 1 is = 
    Value at row 1 is = 2
    Value at row 2 is = 
    Value at row 2 is = 
    Value at row 2 is = 0
    Value at row 2 is = 
    Value at row 2 is = 
    Value at row 2 is = 3
    
        char *arr_input(int limit){
            char *p=(char*)calloc(limit*6,sizeof(char));
            int i=0;
            while(i<limit){
                scanf(" %c",&p[i*6+5]);             
                i++;
            }
            zeroIn(p,limit);
            arr_display(p,limit);
            return p;
        }
        void *zeroIn(char *p ,int limit){
            int i=0,j=0;
            for(i=0;i<limit;i++){
                for(j=0;j<5;j++)
                    p[i*6+j]='0';
            }       
                //arr_display(p,limit); 
        }
        char *arr_display(char *disp,int size){
            int i=0,j=0;
            for (i = 0; i <size ;i++)
            {
                for(j=0;j<6;j++){
             printf("Value at row %d is = %c\n",i,disp[i*6+j]);
                }
            }
        }
    
    Value at row 0 is = 0
    Value at row 0 is = 0
    Value at row 0 is = 0
    Value at row 0 is = 0
    Value at row 0 is = 0
    Value at row 0 is = 1
    Value at row 1 is = 0
    Value at row 1 is = 0
    Value at row 1 is = 0
    Value at row 1 is = 0
    Value at row 1 is = 0
    Value at row 1 is = 2
    Value at row 2 is = 0
    Value at row 2 is = 0
    Value at row 2 is = 0
    Value at row 2 is = 0
    Value at row 2 is = 0
    Value at row 2 is = 3
    
    
    char *p=(char*)calloc(limit*6, sizeof(char)); 
    scanf("%c",&p[i*6+5]);