C-结构数组

C-结构数组,c,arrays,struct,malloc,C,Arrays,Struct,Malloc,我有一个txt文件 abc=123 def=456 我已经这样定义了我的结构: typedef struct rule { char* old; char* new; }Rule; 我通过函数计算了这些规则的数量 int count_规则() 现在我在另一个函数中调用这个函数,同时制作规则字典 void make_dic(){ ammount_rules = count_rules(); //here goes the problem Rule *dictionary

我有一个txt文件

abc=123 def=456

我已经这样定义了我的结构:

 typedef struct rule {
    char* old;
    char* new;
 }Rule;
我通过函数计算了这些规则的数量

int count_规则()

现在我在另一个函数中调用这个函数,同时制作规则字典

void make_dic(){
 ammount_rules = count_rules();
 //here goes the problem
 Rule *dictionary = malloc(ammount_rules * sizeof(Rule));
}
我想扫描另一个txt文件并用新文件替换旧文件,所以我想用简单的命令访问每个twin文件

for (i=0; i<ammount_rules;i++){
  if ( (string_in_text) == (dictionary.old[i]) )
  {
  printf("%s" dictionary.new[i]);
  }
}

for(i=0;i您的
malloc
似乎很好。它使用
amount\u规则
元素分配一个
Rule
数组

然而,您使用
词典的方式似乎是错误的

dictionary.old[i]  // wrong

dictionary[i].old  // correct

dictionary.new[i]  // wrong

dictionary[i].new  // correct

顺便说一句:请注意,您正在比较指针,而不是字符串。使用strcmp比较字符串。

比较您使用的两个字符串,而不是
=
。您有什么问题?您在问什么问题?我知道,谢谢:)我只是简单地展示了mallocing arrayAblenky的内涵:我在创建某种数组时遇到了问题,我可以用它浏览循环并比较字符串“我想扫描另一个txt”,我怀疑
char*old;字符*新缺少用于保存字符串的分配内存将导致问题。谢谢!现在,我在malloc命令下尝试了cycle,并编写了:(用于规则长度){dictionary[i].old='try';printf(“%s”,dictionanry[i].old);}程序停止工作,您能帮助我吗?我现在绝望了,我被编辑了几个小时:哈哈,我明白了,我应该用“”而不是“”我是一片。。。哈哈,谢谢你,伙计!你救了我!再次提醒我,如果我想在字典[i]中添加char而不是string,该怎么办?这使我的程序停止工作:(对于规则的长度){dictionary[i].old[i]='c';printf(“%c”,dictionanry[i].old[i]);}@MirEso我想你应该问一个新问题,这样你就可以发布你现在的代码了。我相信你会得到帮助的。