如何在C中扫描未知数量的整数到数组?

我知道我可以使用scanf一定数量的数字,例如3个数字

 scanf("%d %d %d",array[0],array[1],array[2]); 

但如果我不知道在输入(NOT EOF)之前输入数组有多少数字(整数,不是浮点数),我怎么能扫描呢? 例如

 input : 12 43 23(enter) --> array[0]=12, array[1]=43, array[2]=23 input : 10 20 30 40 50(enter) --> array[0]=10, array[1]=20, array[2]=30, array[3]=40, array[4]= 50 etc.. 

它是关于如何将数字输入整数数组。

如果可能的话,我想将它保存到2维数组中

 input : 12 43 23(enter) --> array[0][0]=12, array[0][1]=43, array[0][2]=23 input : 10 20 30 40 50(enter) --> array[1][0]=10, array[1][1]=20, array[1][2]=30, array[1][3]=40, array[1][4]= 50 

这是代码,显示如何将整数扫描到2D数组:

 #include  #include  #include  #define INITSIZE 5 #define BUFFSIZE 1000 void print_and_free(int **array, int rowsize, int colsize); void check_ptr(void *ptr, const char *msg); int main(void) { int **array; size_t rowsize = INITSIZE, colsize = INITSIZE; int row = 0, col, numdigits; char buffer[BUFFSIZE]; char *number; array = malloc(rowsize * sizeof(*array)); check_ptr(array, "Allocation"); printf("Enter digits(Enter blank line to end):\n"); while (fgets(buffer, BUFFSIZE, stdin) != NULL && strlen(buffer) != 1) { col = 0; numdigits = 0; if (rowsize == row) { rowsize *= 2; array = realloc(array, rowsize * sizeof(*array)); check_ptr(array, "Reallocation"); } array[row] = malloc(colsize *sizeof(int)); check_ptr(array[row], "Allocation"); number = strtok(buffer, " "); while (number != NULL) { numdigits++; if (colsize == numdigits) { colsize *= 2; array[row] = realloc(array[row], colsize * sizeof(int)); check_ptr(array[row], "Reallocation"); } array[row][col] = atoi(number); col++; number = strtok(NULL, " "); } row++; } print_and_free(array, row, col); return 0; } void print_and_free(int **array, int rowsize, int colsize) { int row, col; printf("Your numbers:\n"); for (row = 0; row < rowsize; row++) { for (col = 0; col < colsize; col++) { printf("array[%d][%d] = %d", row, col, array[row][col]); if (col != colsize - 1) { printf(", "); } } free(array[row]); array[row] = NULL; printf("\n"); } free(array); } void check_ptr(void *ptr, const char *msg) { if (!ptr) { printf("Unexpected null pointer: %s\n", msg); exit(EXIT_FAILURE); } } 

以下是如何在(动态分配的)数组中存储内容。 这确实假设线长度限制为1000个字符。 (代码改编自如何使用scanf()获取任意数量的整数? )

 #include  #include  int main() { int val_size = 2; int* vals = (int*)malloc(val_size * sizeof(int)); // initial array size char buffer[1000]; // for reading in the line int pos, bytes_read, num; int num_read = 0; if (fgets(buffer, sizeof(buffer), stdin) != 0) { for (pos = 0; sscanf(buffer+pos, "%d%n", &num, &bytes_read) != EOF; pos += bytes_read) { // resize the array if needed if (num_read >= val_size) { val_size *= 2; vals = (int*)realloc(vals, val_size * sizeof(int)); } // store the value in the array vals[num_read] = num; num_read++; } } // print the values to prove it works for (int i = 0; i < num_read; i++) { printf("%d ", vals[i]); } printf("\n"); free(vals); // important after you're done with it } 

您可以在if周围包裹一段时间以获得多行。

您可以使用while循环并检查scanf的返回值。 例如:

 int num = 0; int idx = 0; int arr[100] = { 0 }; while( scanf( "%d", &num ) == 1 ) { arr[idx++] = num; if( idx == 99 ) // protect from array overflow { break; } } for( int i = 0; i < idx; i++ ) { printf( "%d\n", arr[i] ); }