c - 在 C 中的字符串矩阵上重新分配空间

标签 c pointers matrix

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

#define MAX_STRING_LENGTH 100

int main(int argc, char const *argv[])
{
    FILE* file;
    char ***myTable;
    /* I want to declare a matrix which will contain a string in each cell */
    int end = 1;
    int row = 0;

    /* space declaration */

    myTable = (char ***) malloc(sizeof(char**));   

    myTable[0] = (char **)malloc(3 * sizeof(char*));
    for(int j = 0; j < 3; j++)
        myTable[0][j] = (char *)malloc(MAX_STRING_LENGTH * sizeof(char));
    file = fopen(argv[1], "r");

    while(end) {
        //filling the matrix from the file no matter how many row
        //the file has
        for(int j = 0; j < 3; j++) {
            fscanf(file, "%s", myTable[row][j]);
        }
        //adding one row every time i retrive the data

        myTable = (char***) realloc(myTable, sizeof(**myTable) * (row+1));
        row++;
        if(getc(file) == EOF) end = 0;
    }

    fclose(file);

    for(int rows = 0; rows < row; rows++) {
        for(int col = 0; col < 3; col++)
            printf("numero: %s ", myTable[rows][col]);
        printf("\n");
    }
    return 0;
}

我正在尝试创建一个字符串矩阵,每当文件输入中有新行时,它都会增加行大小。该文件将如下所示。

3333333333 date1 u
2222222222 date2 e

在这种情况下,程序中的矩阵将有 2 行和 3 列。

在这个文件中

3333333333 date1 u
2222222222 date2 e
8888888888 date3 e

在这种情况下,程序中的矩阵将有 3 行和 3 列;列将始终固定,但行数必须每次增加一。

最佳答案

您需要为每一行分配空间。

int main(int argc, char const *argv[])
{
    FILE* file;
    char ***myTable = NULL;
    /* I want to declare a matrix which will contain a string in each cell */
    int end = 1;
    int row = 0;

    file = fopen(argv[1], "r");

    while(end) {
        // TODO: Add NULL ptr check....
        // Allocate space here:
        // First, add a row
        myTable = realloc(myTable, sizeof(char**) * (row + 1));
        // Next, alloc 3 pointers in the new row
        myTable[row] = malloc(3 * sizeof(char*));
        // Then, alloc space for 3 strings
        for(int j = 0; j < 3; j++)
            myTable[row][j] = malloc(MAX_STRING_LENGTH * sizeof(char));

        //filling the matrix from the file no matter how many row
        //the file has
        for(int j = 0; j < 3; j++) {
            fscanf(file, "%s", myTable[row][j]);
        }
        row++;
        if(getc(file) == EOF) end = 0;
    }

    fclose(file);

    for(int rows = 0; rows < row; rows++) {
        for(int col = 0; col < 3; col++)
            printf("numero: %s ", myTable[rows][col]);
        printf("\n");
    }
    return 0;
}

注意:您可以通过创建一个结构来保存数据来简化事情:

struct my_data {
    char id[100];
    char name[100];
    char data[100];
};

然后它简化为:

int main(int argc, char const *argv[])
{
    FILE* file;
    struct my_data * myTable = NULL;
    /* I want to declare a matrix which will contain a string in each cell */
    int end = 1;
    int row = 0;

    file = fopen(argv[1], "r");

    while(end) {
        // TODO: Add NULL ptr check....

        myTable = realloc(myTable, sizeof(struct my_data) * (row + 1));

        //filling the matrix from the file no matter how many row
        //the file has
        fscanf(file, "%s %s %s", myTable[row].id, myTable[row].name, myTable[row].data);
        // TODO: check the return value from fscanf

        row++;
        if(getc(file) == EOF) end = 0;
    }

    fclose(file);

    for(int rows = 0; rows < row; rows++) {
        printf("numero: %s %s %s\n", myTable[rows].id, myTable[rows].name, myTable[rows].data);
    }
    return 0;
}

关于c - 在 C 中的字符串矩阵上重新分配空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53599694/

相关文章:

C:两种数组初始化之间的差异

java - 如何将Java静态二维 double 组代码转换为C代码

pointers - 设置指针类型的结构域

javascript - 循环二维关联数组的最有效方法?

algorithm - n×n零矩阵用1和-1填充的方法使得每行每列只能有一个1和-1,每行每列之和为0

c - 重新连接用 C 编写的 websocket 客户端

c - 1到n之间素数的个数

c - C 中带有指针的 int 矩阵 - 内存分配困惑

c - 指针初始化和两个指针指向同一个地址

c - 使用 CUDA 减少矩阵行