c - 如何将未知数量的整数扫描到 C 中的数组中?

标签 c arrays integer

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

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

但是如果我不知道在输入(不是 EOF)之前要输入数组的数字(整数,而不是 float ),我该如何扫描它?例如

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..

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

如果可能的话,我想将它保存到一个二维数组中,例如

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

最佳答案

下面是显示如何将整数扫描到二维数组中的代码:

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

#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);
    }
}

关于c - 如何将未知数量的整数扫描到 C 中的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40972455/

相关文章:

java - 从 Java 传递到 C 后未初始化的 JNA 结构数组

c - 带有图标对齐的 Gtk3 GMenuItem

python - 如何在 numpy 行上应用通用函数?

integer - Swift:两个最大整数的总和

java - 再现行为 MAX_VALUE 和 MIN_VALUE

c - 返回指向子数组的指针

c - 打开文件时出现段错误

php数字数组选择大于数字且小于另一个的值并将其保存到新数组

javascript - 如何使用 Array.prototype.sort() 对具有重复值的数组进行排序?

Ruby 判断一个字符串是否只包含数字