本文介绍了询问struct中的元素是否为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果我有: struct one_ { unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_ { unsigned int two_1; unsigned short two_2; unsigned char two_3; }; struct mystruct { struct one_ one; struct two_ two; } mystruct1; 然后我可以通过任何更改询问整个struct mystruct1的值, 是一次调用中struct的所有元素?我想做点什么 喜欢(用伪语言): if(mystruct1 == 0){print(" mystruct1的所有元素是零");} 最好的问候 Terry 解决方案 [好东西剪了] 如果你肯定知道的话struct mystruct没有任何填充位 (并且你不能轻易地确定这个),或者如果你知道结构 mystruct总是以一种所有方式初始化它的填充位 为零(例如由calloc()分配,或者通过调用 memset(p,''\ 0'',sz)进行初始化)),你可以使用函数memcmp()。 { static const struct mystruct z; if( memcmp(& mystruct1,& z)){print(" mystruct1的所有元素都为零); } } 但是,理查德的建议是最直接的,而且更不容易发生b $ b错误。 - James 我认为你需要sizeof z作为第三个参数。 :-) - Richard Heathfield: [email protected] Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。 C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K& R答案,C书等: http://users.powernet.co.uk/eton If I have: struct one_{unsigned int one_1;unsigned short one_2;unsigned short one_3;}; struct two_{unsigned int two_1;unsigned short two_2;unsigned char two_3;}; struct mystruct{struct one_ one;struct two_ two;}mystruct1; Then could I by any change ask on the value of the whole struct mystruct1,that is all the elements in the struct in one call? I want to do somethinglike (in pseudo like language): if(mystruct1 == 0) { print("All elements of mystruct1 is zero");}Best RegardsTerry 解决方案 No. You could, however, do this: int CompareMyStructsForEquality(const struct mystruct *ms1,const struct mystruct *ms2){int same = 0;if(ms1->one.one_1 == ms2->one.one_1 &&ms1->one.one_2 == ms2->one.one_2 &&ms1->one.one_3 == ms2->one.one_3 &&ms1->two.one_1 == ms2->two.one_1 &&ms1->two.one_2 == ms2->two.one_2 &&ms1->two.one_3 == ms2->two.one_3){same = 1;}return same;} You can then do: struct mystruct z = {0};if(CompareMyStructsForEquality(&z, &mystruct1) == 1){puts("mystruct1 is zeroed.");} --Richard Heathfield : bi****@eton.powernet.co.uk"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.C FAQ: http://www.eskimo.com/~scs/C-faq/top.htmlK&R answers, C books, etc: http://users.powernet.co.uk/eton No. [good stuff snipped] If you knew for sure that struct mystruct did not have any padding bits(and you can not portably determine this), or if you knew that structmystruct is always initialized in a way such that all its padding bitsare zero (such as allocated by calloc(), or was initialized by a call tomemset(p, ''\0'', sz)), you could have used the function memcmp(). {static const struct mystruct z;if(memcmp(&mystruct1, &z)) { print("All elements of mystruct1 is zero"); }} But, Richard''s suggestion is the most straight forward and lesserror prone. -- James I think you''ll need sizeof z as a third argument. :-) --Richard Heathfield : bi****@eton.powernet.co.uk"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.C FAQ: http://www.eskimo.com/~scs/C-faq/top.htmlK&R answers, C books, etc: http://users.powernet.co.uk/eton 这篇关于询问struct中的元素是否为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-26 22:15