用户定义的数组元素和C中的数组大小

我是C编程的新手,我自己学习。 我想编写一个代码,要求用户输入一些数字并将它们存储到数组中。 如果用户输入“q”,程序将停止。 然后它应该打印数组并告诉用户该数组中有多少个数字。 (长度)

我写了下面的代码,但如果我留下int array []; 空的,它不起作用(显然)。 我无法定义它,因为它取决于用户输入的数量…我通过互联网搜索了很多并遇到了malloc和calloc。 我试着在这里使用它们,但老实说我不知道​​如何,我现在坐在这段代码上几天了。

#include  #include  int main() { int array[]; //I want to leave this empty but C doesn't allow me to. int len=sizeof(array)/sizeof(array[0]); for(int a=0;a<len;a++) { printf("Enter element %d: ", a); scanf("%d",&array[a]); if(getchar()=='q') break; } printf("Array: ["); for(int a=0;a<len-1;a++) { printf("%d, ", array[a]); } printf("%d]", array[len]); printf("\nArray length: %d\n", len); return 0; } 

int数组的样本输出[5];

 Enter element 0: 1 Enter element 1: 2 Enter element 2: 3 Enter element 3: 4 Enter element 4: 5 Array: [1, 2, 3, 4, 5] Array length: 5 

任何帮助都非常感谢。 感谢,并有一个愉快的一天。

Malloc和calloc函数允许您为变量动态分配内存。 我认为如果使用int指针( int* )而不是int []它会更好。 你可以做这样的事情

 int * array = malloc (sizeof(int)); //this will allocate enough memory for 1 int element Len = 1; 

然后在循环结束时,如果用户没有输入’q’,你可以做这样的事情

 len ++ ; array = realloc(array,len*sizeof(int)); //this will reallocate memory for your int pointer 

然后在代码的末尾你必须像这样free(array);地调用函数: free(array); 释放分配的内存。

编辑:在for循环之后,您正在访问不应该在array[len]内存。 我认为你应该把它改成array[len-1]

正如你自己所说,我们的想法是分配一些元素,然后在其中存储元素。 当它满了,然后重新分配并线性增加其大小。 在处理结束时,您释放未使用的内存。

我所说的紧密实施:

 #include  #include  #include  #include  #include  #define MAXLEN 3 #define MAXBUFF 30 int main(void) { size_t sz = 0,tsz=MAXLEN; int *arr= malloc(sizeof*arr*MAXLEN); if( !arr){ perror("malloc:"); exit(EXIT_FAILURE); } char s[MAXBUFF]; while(fgets(s,MAXBUFF,stdin)!=NULL){ if(strstr(s,"q") != NULL){ break; } else{ char *t; errno = 0; long n = strtol(s, &t, 10); if ((errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))|| (errno != 0 && n == 0)) { perror("strtol"); exit(EXIT_FAILURE); } if (t == s) { fprintf(stderr, "No digits were found\n"); exit(EXIT_FAILURE); } if ( n >= INT_MIN && n <= INT_MAX) { arr[sz++]=(int)n; } else{ fprintf(stderr, "Too big/small a number\n"); exit(EXIT_FAILURE); } } if( sz == tsz){ int *p = realloc(arr,sizeof *p*(tsz+MAXLEN)); if(!p){ perror("Realloc:"); exit(EXIT_FAILURE); } arr = p; tsz+= MAXLEN; } } int *p = realloc(arr,sizeof *p*sz); if(!p){ perror("Realloc:"); exit(EXIT_FAILURE); } arr = p; printf("Numbers entered = %zu\n",sz ); for(size_t i = 0; i< sz; i++) printf("a[%zu]=%d\n",i,arr[i] ); free(arr); return(0); } 

在阅读此代码时,我建议查看手册页以了解每个函数返回的内容。 最后一个realloc()基本上将内存缩小到适当的大小(如果分配的数量超过需要的话)。 内存增加是线性的(我们逐渐增加了size )。

没有大小的数组只有在c99为你的问题发挥作用后才允许你可以使用malloc和realloc来动态定义数组的大小这里是程序的运行方式

 #include  #include  int main() { //Initially defining the size of our dynamic array=1 int *arr = malloc(1 * sizeof(int)); int counter=2; while(1) { printf("Enter element %d: ", a); //reallocating array size after user enters a value arr = realloc(arr, counter * sizeof(int)); counter++; scanf("%d",&arr[counter]); if(getchar()=='q') break; } printf("Array: ["); for(int a=0;a 

如果有帮助,请告诉我

Vector更适合您的应用程序。

http://www.cplusplus.com/reference/vector/vector/

打击我之前说的(它在C中不可用)。

C方式基本​​上是使用int指针(int *)malloc默认大小(比如说10个整数)。 并跟踪用户输入的内注量。 当/如果用户输入10个整数,则malloc一个具有更大容量的新数组,复制较小数组中的数据,并释放较小的数组。

由于你是C的新手,想要学习,我避免输入代码。