【发布时间】:2013-10-01 06:38:02
【问题描述】:
根据我的earlier asked question,我正在尝试使用此代码将代码生成的值,即哈希名(在第四个循环中生成的变量,a-z 的组合,四个字符串)传递给结构成员。
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 声明为全局变量。
【问题讨论】:
-
你为什么要使用这个:
sss->Variables[0]。它不应该等于循环运行的次数吗 -
好吧,可以说
NumVariables持有数字值的次数,for 循环生成的值将传递给结构,该怎么做?意味着我没有任何线索。 ..
标签: c structure concatenation