本文介绍了如何检查是否"设置"在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我分配C数组是这样的:

  int数组[5];

然后,只设定一个目标:

 数组[0] = 7;

我如何检查是否所有其它键(数组[1] 数组[2] ,... )是存储价值? (在这种情况下,当然,它们不是。)

有没有像函数PHP的使用isset()

 如果(使用isset(数组[1]))...


解决方案

有没有这样的事情在C.静态数组的内容总是被套。你可以,不过,填写一些特殊的价值pretend是未初始化的,例如

  //确保这个值是不是真的使用。
#定义UNINITIALIZED 0xcdcdcdcdint数组[5] = {未初始化的,未初始化,未初始化,未初始化,未初始化};数组[0] = 7;如果(阵列[1]!=未初始化){
   ...

If I allocate a C array like this:

int array[ 5 ];

Then, set only one object:

array[ 0 ] = 7;

How can I check whether all the other keys ( array[1], array[2], …) are storing a value? (In this case, of course, they aren't.)

Is there a function like PHP's isset()?

if ( isset(array[ 1 ]) ) ...
解决方案

There isn't things like this in C. A static array's content is always "set". You could, however, fill in some special value to pretend it is uninitialized, e.g.

// make sure this value isn't really used.
#define UNINITIALIZED 0xcdcdcdcd

int array[5] = {UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED};

array[0] = 7;

if (array[1] != UNINITIALIZED) {
   ...

这篇关于如何检查是否"设置"在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:06