简体   繁体   中英

how do i know if element in array is there?

So I have an array in my C program

Node *array;
array = (Node*)malloc(53*sizeof(Node));

if(array[5] is empty)
 //Fill array index with element

How do I test if element is there or not? I've tried if(array[5] == NULL) but it keeps giving me an error.

I've tried if(array[5] == NULL) but it keeps giving me an error.

This does not work for two reasons:

  • malloc does not initialize the memory returned to your program. You are responsible for setting memory to zero, and
  • Node is not a pointer type, so you do not know if a Node is valid or not without knowing the structure of Node itself

You could switch to a double-pointer Node **array , and allocate it like this:

array = (Node**)malloc(53*sizeof(Node*));
memset(array, 0, 53*sizeof(Node*));

However, this means that you would have to array as an array of pointers, with malloc s of individual elements and so on.

If you want an array of Node structures, use a separate array of markers that indicate that a particular element is used or not.

All of the elements in the array exist. There's no way to check whether an element exists, because elements can't not exist.

If you want to have some concept of a "nonexistent element", you have to make it yourself. A straightforward way to implement this would be to have a field in Node which stores whether the node "exists". You'd set that field to false in every Node after allocating them, and you'd set it to true whenever you want the node to "exist".

You could initialize the memory with a sentinel value, such as zero, and then check whether array[5] only contains that value. Eg:

#define UNINITIALIZED 0

Node *array = calloc(53, sizeof(Node)); // don't cast malloc and friends!
Node null_node;
memset(&null_node, UNINITIALIZED, sizeof(Node));

if(memcmp(&array[5], &null_node, sizeof(Node)) == 0)
    // …

Just make sure a valid Node instance can never equal to that sentinel value.

This is more efficient than dasblinkenlight's solution, because it involves an additional indirection. However, if a safe sentinel value cannot be found, and adding an int uninitialized (or similar) flag field to Node is not possible, the extra pointer is necessary, unless you store the information about which elements are initialized somewhere else.

I personally think using c++ in such cases is better because then you can use default constructors to initialize. In your case, if you want to check if Node was 'initialized' or not, simply do a calloc and then check value of some integer var.

struct Node {
   //set to 1 if not empty
   char emptyFlag;
   initialize() {

        emptyFlag = 1;

        //other stuff 
   }
}

Node *array;
array = (Node*)calloc(53 , sizeof(Node));

if(array[5].emptyFlag == 0)
 //initialize array[5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM