根据我的earlier asked question,我试图将代码生成的值,即hashname(在第四个循环中生成的变量,az,四个字符串的组合)传递给结构成员,使用此代码。
vcd_xyz[4]='\0';
count = 0;
for(int i=0;i<26;i++)
{
vcd_xyz[0] = 'a'+i;
// printf("%d generated variable is initial is = %c \n",i,vcd_xyz[0]);
for(int j=0;j<26;j++)
{
vcd_xyz[1] = 'a'+j;
// printf("%d generated variable is = %c \n",j,vcd_xyz[1]);
// puts(vcd_xyz);
for(int k = 0;k<26;k++)
{
vcd_xyz[2] = 'a' + k;
// puts(vcd_xyz);
for(int l=0;l<26;l++)
{
vcd_xyz[3] = 'a' +l;
count++;
sss->Variables[0].hashname = (char*)calloc(strlen((char*)vcd_xyz)+1,sizeof(char));
strcpy(sss->Variables[0].hashname,(char*)vcd_xyz);
if(count>(sss->NumVariables))
{
break;
}
}
}
}
}
但是根据它的输出,只有for循环产生的最后一个值被复制到结构中。我无法理解为什么会发生这种情况。虽然打印时vcd_xyz打印了for循环生成的所有值。
Variables
是指向结构Variable
的指针,Name
,NumVariables
和hashname
是其成员。
结构变量是结构sss
的成员
我已将vcd_xyz声明为全局变量。
答案 0 :(得分:1)
这么多问题:
你永远不会增加 好的,你解决了这个问题。 count
。
也许您的意思是sss->Variables[count]
而不是sss->Variables[0]
您的情况应为if(count >= sss->NumVariables)
。
您只有最break
循环中的for
。
您不会检查calloc
是否失败。