如何创建结构 c 的动态矩阵?
How to create dynamic matrix of structs c?
我在制作结构的动态矩阵时遇到了一些麻烦。对于动态矩阵,我的意思不是固定数量的列或行。我有固定数量的列(26 个字母),但我希望每列的行数都发生变化。
这就是我到目前为止所做的...
struct cliente {
char nome[9];
struct cliente* next;
};
typedef struct cliente *ClienteSing;
typedef ClienteSing* Cliente[26];
//I'm allocating memory for the matrix. r is an array that tells me the number of lines for a column.
void initArrayCliente (Cliente a, int* r){
int i=0;
for(i=0;i<26;i++)
a[i]=(ClienteSing) calloc (r[i],sizeof(struct cliente));
}
//I'm implementing a hash, so in case of collision, i make a linked list from a position in the matrix. This function puts a client i want to insert, in the correct position in case of collision.
void ultimoRamo (ClienteSing a, ClienteSing b){
ClienteSing temp;
temp=a;
while (temp->next!=NULL)
temp=temp->next;
temp->next=b;
}
//I create a client b from a str that contains the client name. In case that the position in the matrix is set to null(doesn't have cliente) i insert b there. Otherwise, i will use the previous function to create a linked list from that position. indice is the position i want to insert in to. It's a value generated by my hash
void insere(Cliente a, char* str, int indice){
ClienteSing b;
b= (ClienteSing) malloc (sizeof(struct cliente));
strcpy (b->nome, str);
b->next=NULL;
if (a[str[0]-'A'][indice]==NULL)
{
a[str[0]-'A'][indice]=b;
printf("Livre\n");
}
else {
ultimoRamo(a[str[0]-'A'][indice],b);
printf("Colisão\n");
}
}
我可以毫无问题地编译它,它插入得很好并且没有给我任何分段错误......但是当我打印矩阵中的内容时,它给我垃圾......如果我打印那个插入函数中的相同单元格,它可以毫无问题地打印...你能帮我弄清楚我做错了什么吗?
您的打印代码有误:
for(z=0;z<26;z++)
for(t=0;t<arrayCliente[z];t++)
if(a[z][t].nome==NULL){printf("Fodeu-se");}
else printf("%s",a[z][t].nome);
a[z][t].nome
是char
的数组,不是指向char
的指针。因此,如果 a[z][t]
本身是 NULL
,它只是 NULL
,并且只是偶然,因为 nome
是该结构的第一个成员。
此外,您分配和操作结构数组,而不是结构指针数组。比较a[str[0]-'A'][indice]==NULL
没有意义。
我建议您去掉指针,更重要的是去掉数组 typedefs
,这是一个非常令人困惑的 C 构造。然后使矩阵成为指向结构指针数组的指针数组。
我在制作结构的动态矩阵时遇到了一些麻烦。对于动态矩阵,我的意思不是固定数量的列或行。我有固定数量的列(26 个字母),但我希望每列的行数都发生变化。
这就是我到目前为止所做的...
struct cliente {
char nome[9];
struct cliente* next;
};
typedef struct cliente *ClienteSing;
typedef ClienteSing* Cliente[26];
//I'm allocating memory for the matrix. r is an array that tells me the number of lines for a column.
void initArrayCliente (Cliente a, int* r){
int i=0;
for(i=0;i<26;i++)
a[i]=(ClienteSing) calloc (r[i],sizeof(struct cliente));
}
//I'm implementing a hash, so in case of collision, i make a linked list from a position in the matrix. This function puts a client i want to insert, in the correct position in case of collision.
void ultimoRamo (ClienteSing a, ClienteSing b){
ClienteSing temp;
temp=a;
while (temp->next!=NULL)
temp=temp->next;
temp->next=b;
}
//I create a client b from a str that contains the client name. In case that the position in the matrix is set to null(doesn't have cliente) i insert b there. Otherwise, i will use the previous function to create a linked list from that position. indice is the position i want to insert in to. It's a value generated by my hash
void insere(Cliente a, char* str, int indice){
ClienteSing b;
b= (ClienteSing) malloc (sizeof(struct cliente));
strcpy (b->nome, str);
b->next=NULL;
if (a[str[0]-'A'][indice]==NULL)
{
a[str[0]-'A'][indice]=b;
printf("Livre\n");
}
else {
ultimoRamo(a[str[0]-'A'][indice],b);
printf("Colisão\n");
}
}
我可以毫无问题地编译它,它插入得很好并且没有给我任何分段错误......但是当我打印矩阵中的内容时,它给我垃圾......如果我打印那个插入函数中的相同单元格,它可以毫无问题地打印...你能帮我弄清楚我做错了什么吗?
您的打印代码有误:
for(z=0;z<26;z++)
for(t=0;t<arrayCliente[z];t++)
if(a[z][t].nome==NULL){printf("Fodeu-se");}
else printf("%s",a[z][t].nome);
a[z][t].nome
是char
的数组,不是指向char
的指针。因此,如果 a[z][t]
本身是 NULL
,它只是 NULL
,并且只是偶然,因为 nome
是该结构的第一个成员。
此外,您分配和操作结构数组,而不是结构指针数组。比较a[str[0]-'A'][indice]==NULL
没有意义。
我建议您去掉指针,更重要的是去掉数组 typedefs
,这是一个非常令人困惑的 C 构造。然后使矩阵成为指向结构指针数组的指针数组。