[英]How to set a char inside a struct in C?
How do I allocate memory for a char variable (not a char pointer) inside a struct? 如何在结构内为char变量(不是char指针)分配内存?
(Variable names are in portuguese, sorry if it's kinda confusing) (可变名称使用葡萄牙语,如果造成混淆,请抱歉)
I have this struct: 我有这个结构:
typedef struct node{
char rotulo[10], instrucao[1][2][10], flag;
int simplificado;
struct node *referencias[2];
struct node **antecessores;
int nrAntecessores;
struct node *ant;
struct node *prox;
} Estado;
And this is function insere()
that sets values read from an input file in a new node: 这是
insere()
函数,用于设置从新节点中的输入文件读取的值:
void Insere(char *rotulo, char instrucao[][2][10], int qtdInstrucao, char flag){
int i,j;
Estado *NovoEstado;
NovoEstado = (Estado*)malloc(sizeof(Estado));
NovoEstado->prox = NULL;
NovoEstado->ant = P->ult;
strcpy(NovoEstado->rotulo, rotulo);
NovoEstado->flag = flag;
NovoEstado->antecessores = NULL;
NovoEstado->nrAntecessores = 0;
NovoEstado->simplificado = 0;
for(i=0;i<qtdInstrucao;i++){
realloc(NovoEstado->instrucao, i+1*sizeof(char[2][10]));
strcpy(NovoEstado->instrucao[i][0], instrucao[i][0]);
strcpy(NovoEstado->instrucao[i][1], instrucao[i][1]);
}
}
This NovoEstado->flag = flag;
此
NovoEstado->flag = flag;
isn't working... 没用...
Right after I set it, if I print NovoEstado->flag
i get the right value, but if I put it after that for
by the end of the function, when I print NovoEstado->flag
I get the first char of NovoEstado->rotulo... 紧接着我将它,如果我打印
NovoEstado->flag
我得到正确的值,但如果我把它之后for
由函数的最后,当我打印NovoEstado->flag
我得到NovoEstado-的第一个字符> rotulo ...
The same happens when I try to print flag
in main()
... 当我尝试在
main()
打印flag
,也会发生同样的情况……
I figure that's because I'm not properly allocating memory space to flag
in Insere()
, is that right? 我认为这是因为我没有在
Insere()
正确分配内存空间来进行flag
,对吗? And how do I fix it? 我该如何解决?
I'm pretty sure it's an awful easy question, and that I possibily knew this once, but I forgot and can't find it anywhere... So any help would be very appreciated 我很确定这是一个非常简单的问题,而且我可能一次也知道这一点,但是我忘记了并且在任何地方都找不到它...所以我们将不胜感激
EDIT 编辑
Following ocdecio's tip I created a pointer to an two-dimensional array, in order to have a dinamic 3 dimensional array. 遵循ocdecio的技巧,我创建了一个指向二维数组的指针,以便获得动态3维数组。
My goal is to have a "table" like this: 我的目标是要有一个像这样的“桌子”:
10 chars | 10 chars
|__________|__________|
|__________|__________|
|__________|__________|
Where the number of lines is dinamic, but it's always an array of 2 strings of 10 chars. 行数是动态的,但始终是2个由10个字符组成的字符串的数组。
So now this is what I'm doing in main: 所以现在这是我主要要做的:
char estado[127], rotulo[10], strInstrucoes[117], *conjunto = calloc(21, sizeof(char)), flag;
char (*instrucao)[2][10];
FILE * entrada;
Automato *Aut = (Automato*)malloc(sizeof(Automato));
if((entrada = fopen(argv[1], "r")) != NULL){
CriaAutomato(Aut);
while(fgets(estado, 127, entrada)){
flag = 0;
sscanf(estado,"%[^:]: %[^;]; %c", rotulo, strInstrucoes, &flag);
instrucao = calloc(1, sizeof(char[2][10]));
conjunto = strtok(strInstrucoes,"() ");
for(i = 0; conjunto != NULL; i++){
realloc(instrucao, i+1*sizeof(char[2][10]));
sscanf(conjunto,"%[^,],%s", instrucao[i][0], instrucao[i][1]);
printf("%s || %d\n", instrucao[i][1], i);
conjunto = strtok(NULL, "() ");
}
Insere(Aut, rotulo, instrucao, i, flag);
free(instrucao);
}
fclose(entrada);
But this isn't working... 但这不起作用...
This is the input read from file 这是从文件读取的输入
adsasdfg2: (abc,123) (def,456) (ghi,789);
but even before I call Insere
I'm not assigning the right values to instrucao
the way I want, as this is the output of that printf
但即使在我致电
Insere
之前,我也没有按照我想要的方式为instrucao
分配正确的值,因为这是该printf
的输出
123
454
789
instead of what I'm aiming for 而不是我的目标
123
456
789
What's wrong? 怎么了?
(before someone asks, this is part of a homework, but not the homework. My task is to make a Deterministic Finite Automata minimizer, this is just a bug I'm getting related to data input) (有人问过,这是一门功课的一部分 ,但不是功课。我的任务就是让一个确定有限自动最小化,这只是我越来越相关数据输入错误)
Thanks a whole lot 非常感谢
Your problem is likely to be in this line: 您的问题可能在以下这一行:
realloc(NovoEstado->instrucao, i+1*sizeof(char[2][10]));
There is no need to allocate anything inside your structure because the field instrucao
is statically defined by the statement instrucao[1][2][10]
, it is not a dynamically allocated structure. 不需要在结构内部分配任何内容,因为
instrucao
字段是由语句instrucao[1][2][10]
静态定义的,它不是动态分配的结构。
I think this line is the problem: 我认为这是问题所在:
realloc(NovoEstado->instrucao, i+1*sizeof(char[2][10]));
In the structure the instrucao is defined as a chunk of continuous memory but you are now allocating a memory and assigning the pointer. 在结构中,instrucao被定义为一块连续的内存,但是您现在正在分配内存并分配指针。
Try commenting it out, you don't need to allocate memory for that variable. 尝试注释掉它,您不需要为该变量分配内存。
You don't need to allocate flag. 您不需要分配标志。 It's a char defined inside the struct (as oppose to a pointer).
它是在结构内部定义的一个字符(与指针相反)。
Your error is caused by overwriting flag's contents inside the struct during the for loop where you write to NovoEstado->instrucao
(ie its a memory scribble or overwrite). 您的错误是由在for循环中写入
NovoEstado->instrucao
的for循环中覆盖了struct内部标志的内容引起的(即,它的内存NovoEstado->instrucao
或覆盖)。
Update: as @ocdecio points out, you do not need to realloc inside the loop, as instrucao
is also statically allocated. 更新:@ocdecio指出,您不需要在循环内
instrucao
分配,因为instrucao
也是静态分配的。
I don't know the intent of the code, but it's certainly illegal to do the following: 我不知道代码的意图,但是执行以下操作当然是非法的:
realloc(NovoEstado->instrucao, i+1*sizeof(char[2][10])); realloc(NovoEstado-> instrucao,i + 1 * sizeof(char [2] [10]));
Basically, the containing structure was allocated using malloc, but the inner-member in question is a statically defined array and it can't be rellocated using the heap (because the heap manager tracks only the malloced pointer to node). 基本上,包含结构是使用malloc分配的,但是有问题的内部成员是静态定义的数组,无法使用堆进行重新分配(因为堆管理器仅跟踪指向节点的malloc指针)。
typedef struct node{ char rotulo[10], instrucao[1][2][10], flag; typedef struct node {char rotulo [10], instrucao [1] [2] [10], flag;
If you wish to resize for instrucao dynamically, you must define it as a pointer type and allocate memory for it separately. 如果希望动态调整Instrucao的大小, 则必须将其定义为指针类型,并分别为其分配内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.