Memory DMA内存块未接收输入

Memory DMA内存块未接收输入,memory,Memory,因此,我试图编写一个symple程序,它以int的形式接受输入,并将其添加到内存块中。然后,用户可以选择输入另一个数字,并继续这样做,只要他们愿意。每当我运行该程序时,如果我只选择输入1个数字,则在输出列表时会显示错误的数字,但如果我尝试输入多个数字,则程序会崩溃。任何意见或建议都将受到重视 #include <stdio.h> #include <stdlib.h> int main() { int i, j; int *list; ch

因此,我试图编写一个symple程序,它以int的形式接受输入,并将其添加到内存块中。然后,用户可以选择输入另一个数字,并继续这样做,只要他们愿意。每当我运行该程序时,如果我只选择输入1个数字,则在输出列表时会显示错误的数字,但如果我尝试输入多个数字,则程序会崩溃。任何意见或建议都将受到重视


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

int main()
{

    int i,  j;
    int *list;
    char loop;

    j = 1;

        //alocates memory
        list = calloc(j, sizeof(int));
        if(list == NULL)
        {

            printf("Sorry the memory could not be alocated\n");
            return 0;

        } //end if


    //this is where the user can enter the elements of the list
    for(i = 0;i < j;i++)
    {

        //instructs the user to enter a value
        printf("Enter elemint %d of the number list\n", j);

        //stores value entered
        scanf("%d", & *(list+i));

        printf("Do you want to enter another number?\n Y yes\n N no\n");

        //checks input
        do
        {

            //gets input
            scanf("%1s", &loop);

        } //end do while
        while(loop != 'Y' && loop != 'y' && loop != 'N' && loop != 'n');

        if(loop == 'Y' || loop == 'y')
        {

            j++;

            //reallocates memory
            list = realloc(list, j*sizeof(int));

        } //end if
        else if(loop == 'N' || loop == 'n')
        {

            printf("Okay list complete\n");

        } //end else if
        else
        {

            printf("ERROR");
            return 0;

        } //end else

    } //end for

    printf("You entered\n");

    //prints the elemints of the list
    for(i = 0 ; i < j ; i++)
    {

        printf("%d ", *(list+i));

    } //end for

    free(list);
    return 0;
}


#包括
#包括
int main()
{
int i,j;
int*列表;
字符循环;
j=1;
//孤独的记忆
list=calloc(j,sizeof(int));
if(list==NULL)
{
printf(“对不起,无法定位内存\n”);
返回0;
}//如果结束,则结束
//这是用户可以输入列表元素的地方
对于(i=0;i
编辑 我已经解决了这个问题,但我的解决办法有点奇怪。所以我所做的就是在for循环中初始化索引变量I,而不是在main的开头。 当然,还有更多的注释,我有固定的格式问题,但它们在代码方面并不重要


/*
    Description
This program takes input from the user by useing a resizeable list and then displays the list
    Author
Paul Geoghegan
    Date
25/01/20
*/

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

int main()
{

    int j;
    int *list;
    char loop;

    j = 1;

    //alocates memory
    list = calloc(j, sizeof(int));
    //checks if memory has been allocated correctly
    if(list == NULL)
    {

        printf("Sorry the memory could not be alocated\n");
        return 0;

    } //end if

    //this is where the user can enter the elements of the list
    for(int i = 0;i < j;i++)
    {

        //instructs the user to enter a value
        printf("Enter elemint %d of the number list\n", j);

        //stores value entered
        scanf("%d", &*(list+i));

        printf("Do you want to enter another number?\n Y yes\n N no\n");

        //checks input
        do
        {

            //gets input
            scanf("%1s", &loop);

        } //end do while
        while(loop != 'Y' && loop != 'y' && loop != 'N' && loop != 'n');

        if(loop == 'Y' || loop == 'y')
        {

            //increases j for use in reallocateing the memory block
            j++;

            //reallocates memory
            list = realloc(list, j*sizeof(int));

        } //end if
        else if(loop == 'N' || loop == 'n')
        {

            printf("Okay list complete\n");

        } //end else if
        else
        {

            printf("ERROR");
            return 0;

        } //end else

    } //end for

    printf("You entered\n");

    //prints the elemints of the list
    for(int i = 0 ; i < j ; i++)
    {

        printf("%d ", *(list+i));

    } //end for

    //frees up memory and ends program
    free(list);
    return 0;
} //end program


/*
描述
该程序通过使用可调整大小的列表从用户处获取输入,然后显示该列表
作者
保罗·杰根
日期
25/01/20
*/
#包括
#包括
int main()
{
int j;
int*列表;
字符循环;
j=1;
//孤独的记忆
list=calloc(j,sizeof(int));
//检查内存是否已正确分配
if(list==NULL)
{
printf(“对不起,无法定位内存\n”);
返回0;
}//如果结束,则结束
//这是用户可以输入列表元素的地方
对于(int i=0;i