C 程序没有结束,什么也没有发生

C 程序没有结束,什么也没有发生,c,C,编写一个程序,分为带参数的函数,它将加载一个动态整数数组,直到用户输入数字0。最初,假设数组只包含5个这样的数字。如果用户试图输入的数字超过了表所能存储的数量,程序应检测到这种情况,并将其大小再增加5个元素。每当表格在表格中结束时,程序应重复上一句中描述的步骤,直到用户输入完数字。毕竟,程序应该在屏幕上写入表的内容并释放分配的内存 void fill_array(int *); void print_array(int *, int); void *add_memory(int); i

编写一个程序,分为带参数的函数,它将加载一个动态整数数组,直到用户输入数字0。最初,假设数组只包含5个这样的数字。如果用户试图输入的数字超过了表所能存储的数量,程序应检测到这种情况,并将其大小再增加5个元素。每当表格在表格中结束时,程序应重复上一句中描述的步骤,直到用户输入完数字。毕竟,程序应该在屏幕上写入表的内容并释放分配的内存

     void fill_array(int *);
void print_array(int *, int);
void *add_memory(int);
int main()
{
    int x;
    int *array_pointer = (int*)calloc(5,sizeof(int));
    if(array_pointer)
    {
        fill_array(array_pointer);
        x = sizeof(array_pointer)/sizeof(array_pointer[0]);
        print_array(array_pointer, x);
    }

    return 0;
}

void fill_array(int *array)
{
    int i = 0, k = 5;
    printf("Please fill an array with at least 5 digits: ");

    while(scanf("%d", &array[i]) != 0)
    {
        if(i > 5)
        {
            k++;
            array = (int*)add_memory(k);

        }
        i++;
    }
}

void *add_memory(int a)
{

    void *array_ptr = realloc(array_ptr,a*sizeof(int));
    return array_ptr;
}

void print_array(int *array, int b)
{
    int i;
    for(i=0;i< b;i++)
        printf("%d ",array[i]);
    puts("");
}
void fill_数组(int*);
无效打印数组(int*,int);
void*添加内存(int);
int main()
{
int x;
int*数组_指针=(int*)calloc(5,sizeof(int));
if(数组\指针)
{
填充数组(数组指针);
x=sizeof(数组指针)/sizeof(数组指针[0]);
打印数组(数组指针,x);
}
返回0;
}
空填充数组(整数*数组)
{
int i=0,k=5;
printf(“请用至少5位数字填充数组:”;
而(scanf(“%d”)和数组[i])!=0)
{
如果(i>5)
{
k++;
数组=(int*)加上内存(k);
}
i++;
}
}
void*添加内存(int a)
{
void*array_ptr=realloc(array_ptr,a*sizeof(int));
返回数组\u ptr;
}
无效打印_数组(int*数组,int b)
{
int i;
对于(i=0;i
问题代码是
而(scanf(“%d”)和数组[i])!=0)

让我们看看
scanf的返回值

NAME
       scanf,  fscanf, sscanf, vscanf, vsscanf, vfscanf 

       ...

RETURN VALUE
       These functions return the number of input items  successfully  matched
       and assigned, which can be fewer than provided for, or even zero in the
       event of an early matching failure.

       The value EOF is returned if the end of input is reached before  either
       the  first  successful conversion or a matching failure occurs.  EOF is
       also returned if a read error occurs, in which case the error indicator
       for  the  stream  (see ferror(3)) is set, and errno is set indicate the
       error.
因此,在您的情况下,
scanf()
在连续输入整数时始终返回
1

顺便说一句,代码中还有其他问题

  • x=sizeof(数组指针)/sizeof(数组指针[0])

    array\u pointer
    是指针,因此
    sizeof(array\u pointer)
    将返回指针大小(32或64位或其他位),而不是数组大小

  • array=(int*)添加内存(k)

    此代码将重新分配内存,因此当
    fill\u array
    功能完成时<代码>数组\指针
  • 可以是指向已释放地址的指针

  • void*array\u ptr=realloc(array\u ptr,a*sizeof(int))

    array_ptr
    是一个未初始化的变量


  • 此函数没有意义:

    void *add_memory(int a)
    {
    
      void *array_ptr = realloc(array_ptr, a * sizeof(int));
      return array_ptr;
    }
    
    您可以仅在先前由
    malloc
    calloc
    或另一个
    realloc
    分配的指针或
    NULL
    指针上调用
    realloc
    。在您的代码中,
    array\u ptr
    尚未初始化,使用未初始化的指针调用
    realloc
    不会有好的结果


    但是你的课程的整体结构很差,肯定还有其他问题,而不是答案中的问题

    这是
    填充数组的工作代码

    #include <stdlib.h>
    #include <stdio.h>
    
    #define NUMBERTHRESHOLD 5
    
    int *fill_array(int *nbvalues)
    {
      int *array_ptr = NULL;  // hint: read up the specs of realloc
      int actualsize = 0;    // actual size of dynamic array
      int index = 0;         // index of next number to be stored in array
    
      // initially the array is "full", it contains zero numbers
      // and there is there is space left for zero numbers
    
      while (1)          // loop forever
      {
        int v;
        scanf("%d", &v);
    
        if (v == 0)
          break;           // user entered 0 => we stop the loop
    
        if (actualsize == index)
        {
          // the array is full, we need to allocate space for NUMBERTHRESHOLD more numbers
          actualsize += NUMBERTHRESHOLD;
          array_ptr = realloc(array_ptr, actualsize * sizeof(int));   // reallocate memory
        }
    
        array_ptr[index++] = v; // store number entered by user
      }
    
      *nbvalues = index - 1;   // index - 1 is the actual number of numbers in array
      return array_ptr;        // return the pointer to first element of array
    }
    
    int main(void)
    {
      int nbvalues;
      int *array_ptr = fill_array(&nbvalues);
    }
    
    #包括
    #包括
    #定义NUMBERTHRESHOLD 5
    int*fill_数组(int*nbvalues)
    {
    int*array_ptr=NULL;//提示:读取realloc的规范
    int actualsize=0;//动态数组的实际大小
    int index=0;//要存储在数组中的下一个数字的索引
    //最初,数组是“满的”,它包含零个数字
    //还有空间可以容纳零个数字
    while(1)//永远循环
    {
    INTV;
    scanf(“%d”和“&v”);
    如果(v==0)
    break;//用户输入0=>我们停止循环
    如果(实际值==索引)
    {
    //数组已满,我们需要为数字分配空间重新保存更多数字
    实际值+=数字保留;
    array_ptr=realloc(array_ptr,actualsize*sizeof(int));//重新分配内存
    }
    数组_ptr[index++]=v;//用户输入的存储编号
    }
    *nbvalues=index-1;//index-1是数组中的实际数字数
    return array_ptr;//返回指向数组第一个元素的指针
    }
    内部主(空)
    {
    int值;
    int*array\u ptr=填充数组(&nbvalues);
    }
    
    需要做的事情:

    • 您需要编写
      print\u array
      函数并调用它
    • 你需要释放内存
    • 绝对没有进行错误检查(这对于第一次拍摄来说很好)

    以下是代码的功能版本。您必须输入
    0
    停止程序并打印获得的信息。虽然还没有完全完成。你必须做以下事情

  • 错误检查
  • 表面变化,即写评论等
  • 释放资源
  • 尽管看起来不需要更多的解除分配

    #include <stdio.h>
    #include <stdlib.h>
    
    void fill_array(int *);
    void print_array(int *, int);
    
    int *add_memory(int **, int);
    int k = 5;
    
    int main()
    {
        int x;
        int *array_pointer = (int*) calloc(k,sizeof(int));
    
        if(array_pointer)
        {
            fill_array(array_pointer);
            print_array(array_pointer, k);
        }
    
        if(array_pointer)
        {
        /* Deallocate the memory. */
        free(array_pointer);
        }
    
        return 0;
    }
    
    void fill_array(int *array)
    {
        int i = 0;
        int number;
        printf("Please fill an array with at least 5 digits:");
        while(1)
        {
            printf("\r\nEnter a number: ");
            scanf("%d", &number);
    
            if (number == 0)
            {
                break;
            }
            else
            {
                if (i >= 4)
                {
                    k++;
                    array = (int*) add_memory(&array, k);
    
                    if (!array)
                    {
                        break;
                    }
    
                    array[i] = number;
                }
                else
                {
                    array[i] = number;
                }
    
                i++;
            }
    
        }
    }
    
    int *add_memory(int **array_ptr, int a)
    {
        printf("Allocating more memory\r\n");
    
        *array_ptr = realloc(*array_ptr, a*sizeof(int));
    
        if (!*array_ptr)
        {
            printf("Failed to allocate memory.\r\n");
        }
    
        return *array_ptr;
    }
    
    void print_array(int *array, int b)
    {
        int i;
        printf("\r\nPrinting array\r\n");
    
        for(i=0;i< b;i++)
            printf("%d ",array[i]);
        puts("");
    }
    
    #包括
    #包括
    空填充数组(int*);
    无效打印数组(int*,int);
    int*添加内存(int**,int);
    int k=5;
    int main()
    {
    int x;
    int*数组_指针=(int*)calloc(k,sizeof(int));
    if(数组\指针)
    {
    填充数组(数组指针);
    打印数组(数组指针,k);
    }
    if(数组\指针)
    {
    /*释放内存*/
    空闲(数组\指针);
    }
    返回0;
    }
    空填充数组(整数*数组)
    {
    int i=0;
    整数;
    printf(“请用至少5位数字填充数组:”;
    而(1)
    {
    printf(“\r\n输入一个数字:”);
    scanf(“%d”和编号);
    如果(数字==0)
    {
    打破
    }
    其他的
    {
    如果(i>=4)
    {
    k++;
    数组=(int*)添加内存(&array,k);
    if(!数组)
    {
    打破
    }
    数组[i]=个数;
    }
    其他的
    {
    数组[i]=个数;
    }
    i++;
    }
    }
    }
    int*添加内存(int**array\u ptr,int a)
    {
    printf(“分配更多内存\r\n”);
    *array_ptr=realloc(*array_ptr,a*sizeof(int));
    如果(!*阵列\u ptr)
    {
    printf(“未能找到一个