C - 结构内指针的默认值

发布于 2024-11-05 03:49:54 字数 1045 浏览 1 评论 0原文

我对 C 中的单个链表有以下非常简单的实现:

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


void printLL(node * start);
void addNode(node * head, node * add);

int main()
{
    node first;
    first.data = 99;
    first.next = NULL;

    node second;
    second.data = 11;

    addNode(&first, &second);   
    printLL(&first);    
}

void addNode(node * head, node * add)
{
    if(head->next)
    {
        addNode(head->next, add);
    }
    else 
    {
        head->next = add;
    }
}

void printLL(node * start)
{
    printf("Data is %d\n", start->data);
    if (start->next) {
        printLL(start->next);
    }
}

我感到困惑的是,如果我没有明确设置first.next = NULL,我会收到 EXE_BAD_ACCESS 错误。当我尝试检查该属性是否已设置以确定是否应该递归调用时,就会发生这种情况。我不明白的另一件事是,如果我将 first.next 设置为指向“第二”,则所有函数都可以工作,即使对于“第二”我没有明确将其设置为 NULL 旁边。因此,结构中定义的指针的默认值似乎存在一些不一致。当然,我可能做了一些完全错误的事情,但如果有人能阐明这个问题,我将不胜感激。归结起来,我的问题是:

  1. 结构中定义的指针的默认值是什么?
  2. 假设没有默认值,是否有一种简单的方法可以在结构定义中设置默认值? (我翻阅了一些C书籍,没有找到这个问题的答案)

I have the following very simplistic implementation for a single linked list in C:

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


void printLL(node * start);
void addNode(node * head, node * add);

int main()
{
    node first;
    first.data = 99;
    first.next = NULL;

    node second;
    second.data = 11;

    addNode(&first, &second);   
    printLL(&first);    
}

void addNode(node * head, node * add)
{
    if(head->next)
    {
        addNode(head->next, add);
    }
    else 
    {
        head->next = add;
    }
}

void printLL(node * start)
{
    printf("Data is %d\n", start->data);
    if (start->next) {
        printLL(start->next);
    }
}

What I'm confused about is that if I don't explicitly set first.next = NULL, I get a EXE_BAD_ACCESS error. It happens when I try and check if that property is set or not to determine if the call should be made recursively. The other thing I don't understand is that if I set first.next to point to 'second', all functions work even though for 'second' I don't explicitly set it's next to NULL. So it seems as though there is some inconsistency in the default value for a pointer defined within a struct. Of course I'm probably doing something totally wrong but would be grateful if somebody could shed light on the matter. To boil everything down my questions are:

  1. What is the default value for a pointer defined within a struct?
  2. Assuming that there is no default, is there a simple way to set a default within the structs definition? (I looked through some C books and couldn't find an answer to this question)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

云朵有点甜 2024-11-12 03:49:54

因此,结构中定义的指针的默认值似乎存在一些不一致。

这正是问题所在 - 编译器不会将自动(本地)变量设置为任何默认值。因此,您可能会得到一些导致 EXE_BAD_ACCESS 的结果,或者您可能会得到一些看起来有效的东西。这是幸运的(即使它看起来有效,也是一个错误)。

C 的缺点之一是您有责任确保变量正确初始化。编译器可以协助发出警告,但并非所有编译器在这方面都与其他编译器一样好。

有没有一种简单的方法可以在结构定义中设置默认值?

在结构体的定义中没有办法做到这一点。但是,您可以在声明该类型的变量时初始化该结构:

node first = { 99, NULL };

So it seems as though there is some inconsistancy in the default value for a pointer defined within a struct.

That's exactly the problem - the compiler is does not set an automatic (local) variable to any default. So you may get something that results in EXE_BAD_ACCESS, or you may get something that appears to work. It's luck of the draw (and is a bug even if it appears to work).

One of the drawbacks to C is that you are responsible for ensuring that your variables are initialized properly. A compiler may assist with warnings, not all compilers are as good as others in that regard.

is there a simple way to set a default within the structs definition?

There is not a way to do this in the definition of a struct. However, you can initialize a struct when a variable of that type is declared:

node first = { 99, NULL };
拥抱没勇气 2024-11-12 03:49:54

任何指针都没有默认值,它包含一个垃圾值。除非您使用自己的默认值初始化所有指针。

There's no default value for any pointer, it contains a garbage value. Unless you initialize all pointers with your own default value.

べ映画 2024-11-12 03:49:54

结构中的任何内容都没有默认值(除非它是静态创建的)。并且没有办法提供一个 - 如果您想要这样做,您应该使用 C++,它提供了构造函数来准确地做到这一点。如果坚持使用 C,则需要显式初始化该结构。

There is no default value for anything within a struct (unless it is created statically). And there is no way of providing one - if you want that, you should use C++, which provides constructors to do exactly that. If sticking with C, you need to initialise the struct explicitly.

み格子的夏天 2024-11-12 03:49:54

如果您想要默认指针值,您可以创建一个工厂方法,该方法返回结构的新实例,并将值设置为您所需的默认值。

If you want default pointer values, you can create a factory method that returns a new instance of the struct with the values set to your desired default values.

家住魔仙堡 2024-11-12 03:49:54

没有默认值。除非显式修改,否则每个内存位置都将保留其具有的任何值。尽管此行为本身取决于编译器。最重要的是,您不能假设它将包含特定值。

要设置默认值,一个好的做法是在创建 struct 属性的实例时显式为其赋值。

如果您使用C++,您将拥有可以/应该设置成员默认值的构造函数。

There is no default value. Each memory location will hold on to whatever value it has unless explicitly modified. Though this behavior itself is compiler dependent. The bottom line, you cant assume that it will contain a particular value.

To set a default value, a good practice is to explicitly assign values to the struct attributes whenever you create an instance of it.

If you were using C++, you would have constructors where you would/should set default values of the members.

十雾 2024-11-12 03:49:54

在调用 addNode(&first, &second); 时,在 if 条件下,您正在执行 head->next ,其中是什么导致了问题。 next 默认情况下未初始化,并且在运行时导致 EXE_BAD_ACCESS。

但是,当您将其设置为 NULL 时,if 语句中的条件将失败,并让您进入实际分配指针的 else 语句。

On call to addNode(&first, &second);, in the if condition, you are doing head->next which is what causing the problems. next is not initialized by default and is causing EXE_BAD_ACCESS at run time.

But when you set it to NULL, the condition in if statement fails and is getting you to the else statement where you are actually assigning the pointer.

筱果果 2024-11-12 03:49:54

自动变量在 C 中根本不初始化,无论它们是否是 struct 。您必须自己初始化它们。

但与这里的大多数答案相比,似乎暗示任何数据类型(包括指针)的初始化都有一个默认值。这样做就像任何组件都由纯值“0”显式初始化一样。对于你的例子

node first = { .data = 99 };

就可以了。然后保证 next 被正确初始化。 (如果您没有 C99,请省略 .data = 并希望永远不要更改 struct 的布局:)

还要注意,这可能与归零不同带有 memset 左右的 struct 。在某些体系结构上,全 0 位模式可能不是空指针或 double 的正确初始化。因此最好坚持语言本身提供的初始化机制。

Automatic variables are not initialized at all in C, regardless whether they are struct or not. You have to initialize them yourself.

But in contrast to what most answer here seem to imply there is a default for initialization of any data type, including pointers. This is done as if any component would be explicitly initialized by a plain value '0'. For your example

node first = { .data = 99 };

would have done the trick. next is then guaranteed to be initialized properly. (If you don't have C99 omit the .data = and hope that you never change the layout of your struct :)

Observe also that this might be different from zeroing out a struct with memset or so. On some architectures the all 0 bit pattern might not be the correct initialization for a null pointer or a double. So better stick to what the language itself provides as initialization mechanism.

⒈起吃苦の倖褔 2024-11-12 03:49:54

默认值可能取决于 C 编译器,因此最好提供它们。

您必须在创建结构体实例后初始化它,例如 second.next = NULL

Default values might depent on C compiler so it mostly better to provide them.

You have to init it after you created your struct instance like second.next = NULL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文