首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

结构体循环赋值的有关问题

2012-03-12 
结构体循环赋值的问题!我定义了一个结构体,大致结构如下:typedefstruct{ucharnumuchardev:ucharIQuintio

结构体循环赋值的问题!
我定义了一个结构体,大致结构如下:
typedef   struct{
          uchar   num;                    
          uchar   dev:                
          uchar   IQ;                            
          uint     io;                      
          uint   bss;                                
          uchar   check[20];                
          uchar   stop;                    
          uchar   len;                    
          uchar   ve_type[9];          
          uchar   sub_ce[32];        
}TR;

TR   *p_str=(TR   *)malloc(sizeof(TR));
我想用一个循环语句来给*p_str所指向的结构体手动输入赋值,可是考虑了好久也没有好的办法,请求各位大虾指点!

[解决办法]
好像没什么好办法!
只能p_str-> num=xx这样赋值!
[解决办法]
不能取巧吧,变量类型和长度都不一样。
写循环还是需要判断是第几个输入的。
[解决办法]
省不了的也没办法
我昨天也写了一个,为了省事全部弄成string
然后在转,也麻烦

[解决办法]
不好意思,纠正:
int i=0
while(i++ <60)
{
if(i==4 || i==6)
i++;
scanf( "%d ",*(p_str+i));
}

[解决办法]
void main{
int i;
TR *p_str=(TR *)malloc(sizeof(TR));

for(i=0;i <10;i++)
{
switch(i)
{
case 0 :
printf( "Please enter the num of the struct TR (type : unsigned char ):\n ");
if(scanf( "%c ",&p_str-> num) == 0)
printf( "Can not get the num :\n ");
break;
case 1 :
printf( "Please enter the dev of the struct TR (type : unsigned char ):\n ");
if(scanf( "%c ",&p_str-> dev) == 0)
printf( "Can not get the dev :\n ");
break;
case 2 :
printf( "Please enter the IQ of the struct TR (type : unsigned char ):\n ");
if(scanf( "%c ",&p_str-> IQ) == 0)
printf( "Can not get the IQ :\n ");
break;
case 3 :
printf( "Please enter the io of the struct TR (type : unsigned char ):\n ");
if(scanf( "%c ",&p_str-> io) == 0)
printf( "Can not get the io :\n ");
break;
case 4 :
printf( "Please enter the bss of the struct TR (type : unsigned char ):\n ");
if(scanf( "%c ",&p_str-> bss) == 0)
printf( "Can not get the bss :\n ");
break;
case 5 :
printf( "Please enter the check of the struct TR (type : unsigned char ):\n ");
if(scanf( "%[^\n] ",&p_str-> check) == 0)


printf( "Can not get the check :\n ");
break;
case 6 :
printf( "Please enter the stop of the struct TR (type : unsigned char ):\n ");
if(scanf( "%c ",&p_str-> stop) == 0)
printf( "Can not get the stop :\n ");
break;
case 7 :
printf( "Please enter the len of the struct TR (type : unsigned char ):\n ");
if(scanf( "%d ",&p_str-> len) == 0)
printf( "Can not get the len :\n ");
break;
case 8 :
printf( "Please enter the ve_type of the struct TR (type : unsigned char ):\n ");
if(scanf( "%[^\n] ",&p_str-> ve_type) == 0)
printf( "Can not get the num :\n ");
break;
case 9 :
printf( "Please enter the sub_ce of the struct TR (type : unsigned char ):\n ");
if(scanf( "%[^\n] ",&p_str-> sub_ce) == 0)
printf( "Can not get the sub_ce :\n ");
break;

default : break;

}
}
}

应该差不多 只给你提供一个思路 里面还有很多不足
希望咱们一起进步

热点排行