检查是否结构的数组是空的或不 [英] Checking if an array of structs is empty or not

查看:116
本文介绍了检查是否结构的数组是空的或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何做到这一点?例如:

How do I do this? For example

        typedef struct node
     {   int data;
         struct node* next;
     }node;

是我的节点,在我主我有

Is my node, and in my main I have

       int main(){
          node* array[10];
          if (node[0].data == 0){
           ...

                                }

我真的不知道什么,我需要在这里做。我希望能够以检查阵列中的这些条目已经被修改。我会怎么做呢?我曾尝试 - >运算符代替。这是造成我一些混乱,因为我与对象的工作不是吗?我不清楚这一点。

I am not really sure what I need to do here. I want to be able to check if these entries in the array have been modified already. How would I do this? I have tried the -> operator instead of . which is causing me some confusion because I am working with an object aren't I? I am uncertain of this.

以下建议,这是我现在有。

Following advice this is what I now have.

 int main() {
 struct node* arr[10] = { 0 } ;
   addTo(data, arr);
            }


   addTo(int data, node* arr){
        if (arr[0] == NULL)
                             }

最后一行是分段错误。

The last line is a segmentation fault.

推荐答案

在C数组不能是空。他们是永远不会空。如果你声明的10个元素的数组,你将永远有10个元素的数组。有没有办法说一些元素是否已经被修改过,除非你自己拿出来手动标记的元素,你修改一些方法。例如,您可以选择一些保留的元素值,这将指定一个空的元素。

Arrays in C cannot be "empty". They are never empty. If you declared an array of 10 elements you will always have an array of 10 elements. There's no way to say whether some element has been modified or not unless you yourself come up with some way to manually "mark" elements that you modify. For example, you can choose some reserved element value, which will designate an "empty" element.

在你的情况你声明的10指针数组

In your case you declare an array of 10 pointers

node* array[10];

如果您提供的初始化

node* array[10] = { 0 };

您的数组元素将空的初始值。您可以使用该值作为一个空元素的标志。

your array elements will have the initial value of null. You can use that value as a mark of an "empty" element.

这篇关于检查是否结构的数组是空的或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆