Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:表格电子表格数据结构_C_Data Structures_Structure_Spreadsheet_Typedef - Fatal编程技术网

C:表格电子表格数据结构

C:表格电子表格数据结构,c,data-structures,structure,spreadsheet,typedef,C,Data Structures,Structure,Spreadsheet,Typedef,我想做一个动态的数据结构,可以读取和存储一个简单的电子表格。 我创建了一个存储一个单元格的结构,一行的结构,其中我存储指向该行中每个单元格的指针,以及整个表的结构,其中包含指向行的指针 typedef struct cell{ int ceLen; char *cont; }*Cell; typedef struct { int clsinRow; Cell *row; }Row; typedef struct { int rwsinTab; Row *tab; }T

我想做一个动态的数据结构,可以读取和存储一个简单的电子表格。 我创建了一个存储一个单元格的结构,一行的结构,其中我存储指向该行中每个单元格的指针,以及整个表的结构,其中包含指向行的指针

typedef struct cell{
  int ceLen;
  char *cont;
}*Cell;

typedef struct {
  int clsinRow;
  Cell *row;
}Row;

typedef struct {
  int rwsinTab;
  Row *tab;
}Table;
我编写了一个简单的代码来测试行结构,但它似乎不起作用

  char *str1[5] = {"r1c1", "r1c2", "r1c3", "r1c4", "r1c5"}; // Cells content for test purpose only

  Row *r;
  r->clsinRow = 5; //Num of cells in row

  for(int i=0; i<r->clsinRow; i++){
    Cell c = (Cell) malloc(sizeof(struct cell)); //Allocate the space for one cell 
    strcpy(c->cont, str1[i]); 
    
    r->row[i] = c;
  }

  for(int i=0; i<r->clsinRow; i++){
    printf("%s\t", r->row[i]->cont);
  }
char*str1[5]={“r1c1”、“r1c2”、“r1c3”、“r1c4”、“r1c5”};//单元格内容仅用于测试目的
行*r;
r->clsinRow=5//行中的单元格数
for(int i=0;iclsinRow;i++){
Cell c=(Cell)malloc(sizeof(struct Cell));//为一个单元格分配空间
strcpy(c->cont,str1[i]);
r->行[i]=c;
}
for(int i=0;iclsinRow;i++){
printf(“%s\t”,r->row[i]->cont);
}
我不确定这个结构是否得到了正确的实现,是否应该更好地使用其他数据结构来解决这个问题(比如链表或哈希表)。
感谢您的帮助。

首先,您没有为Row变量分配任何空间。你必须分配内存,就像你分配电池一样

但是,即使您修复了它,strcpy也会出现问题。此时,作为char指针的c->cont为空。因此,strcpy很难处理字符串,它返回segfault


我建议您创建一个不同的函数,在这个函数中分配所有需要分配的内容,并返回一个单元格指针(cell)。然后给这个指向c变量的指针,希望它能工作。

出于好奇
clsinRow
这是什么样的命名约定?为什么不干脆
行数
clsin
代表什么
typedef*单元格
-typedef指针(几乎)总是一个坏主意,不要使用它。这是令人困惑的,就像你的例子-
Cell*row是一个双指针<代码>行*r;r->
-无效-
r
未初始化。