无法从通过生成的值for循环结构 [英] Unable to pass the generated values from a for loop to a structure
问题描述
按我的<一个href=\"http://stackoverflow.com/questions/19089395/c-$c$c-to-generate-variable-with-the-combination-of-a-z-and-max-of-4-char\">earlier被问到的问题,我试图通过从$ C $产生单位c即hashname(变量,在第四循环产生,AZ,一个四个字符的字符串的组合)结构体成员,使用这个code。
As per my earlier asked question , i am trying to pass values generated from the code i.e. hashname(variables which are generated in the forth loop, combination of a-z, a four character string) to a structure member, using this code.
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循环在structure.I被复制生产的最后一个值是无法理解这是为什么在打印时打印全部由循环生成的值happening.Although vcd_xyz。
But as per its output, only last value produced by the for loop is being copied in the structure.I am unable to understand why this is happening.Although vcd_xyz when printed print all the values generated by for loop.
变量
是一个指针指向的结构变量
和名称
, NumVariables
和 hashname
都是其成员。
结构变量是结构的成员 SSS
我宣布vcd_xyz为全局变量。
Variables
is a pointer pointing to a structure Variable
and Name
,NumVariables
and hashname
are its members.
Structure Variable is a member of structure sss
i had declared vcd_xyz as a global variable.
推荐答案
这么多的问题:
-
你永远不增加的好吧,你固定的。的计数
。
也许你的意思 sss-&GT;变量[计数]
而不是 sss-&GT;变量[0]
和你的情况应该是如果(计数&GT; = sss-&GT; NumVariables)。
您只能破
出的最内最为
-loop。
You only break
out of the most inner-most for
-loop.
您不检查,如果你的释放calloc
失败。
You don't check if your calloc
fails.
这篇关于无法从通过生成的值for循环结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!