【问题标题】:Obtain data deep in structures C获取结构 C 深处的数据
【发布时间】:2018-04-24 16:34:10
【问题描述】:

我刚刚遇到了一些很奇怪的事情。 当我有一个指向包含另一个指向相同类型结构的指针的结构的指针时,如果我想从“第二个”结构中获取数据(如果你没有明白我的意思,也许下图将描述更好)...

                      1
          ptr ---> |------|       2
                   | next ---> |------|
                   | data |    | next |
                   |------|    | data | <= data which i desire to get 
                               |------|

如果我真的不想声明另一个指针变量,我应该如何访问所需的数据?

如果想要的数据更“深”怎么办?

我尝试了类似的东西进行测试,但感觉有点......“看起来很糟糕”,我对没有编译器警告这一事实感到非常惊讶:

ptr2struct.c

#include <stdio.h>
#include <stdlib.h>

typedef struct Structure structure;
struct Structure {
    int data;
    structure *next;
};

int main()
{
    structure *ptr = malloc(sizeof(*ptr));

    /* First structure */
    ptr->data = 1;
    ptr->next = malloc(sizeof(ptr->next));

    /* Second structure */
    ptr->next->data = 2;
    ptr->next->next = malloc(sizeof(ptr->next->next));

    /* Third structure, why not */
    ptr->next->next->data = 3;
    ptr->next->next->next = NULL;

    printf("ptr->data = %d\n", ptr->data);
    printf("ptr->next->data = %d\n", ptr->next->data);
    printf("ptr->next->next->data = %d\n", ptr->next->next->data);

    return 0;
}

我想有很多更好的方法可以做到这一点,我知道最好的方法可能是遍历或声明额外的指针,但是如果约束不允许这样的方法怎么办? 实现这一目标的总体最佳方法是什么?

顺便说一句,这不是我的硬件,只是好奇:)

祝您有美好的一天,感谢您的提示!

【问题讨论】:

  • 我很惊讶它也能正常工作,因为 malloc(sizeof(ptr)) 是错误的 - 它获取指针的大小而不是指针的大小
  • 我不完全确定你在问什么。似乎它可以冒险提出基于意见的答案。您基本上描述了一个链接列表,但并没有真正描述一个问题或问题,因为您的所有约束都是理论上的且定义不明确。
  • malloc(sizeof(ptr-&gt;next)); 一样,也是指针的大小,而不是结构。还有malloc(sizeof(ptr-&gt;next-&gt;next))等。“更好”的方法是从更好地理解sizeof开始。
  • 您探索过基于宏的解决方案吗?它可以帮助清理一下语法。
  • 更好的方法?我不知道这是否更好,但它看起来很优雅。创建一个链表结构,其唯一的具体数据成员是指向链表头的指针。还将函数指针添加到 get 函数,该函数接受一个关于您想要进入列表的深度并返回所需深度的数据的 int 参数。但正如有人指出的那样,这是我的意见。你正在做的事情要普遍得多

标签: c pointers structure


【解决方案1】:

首先错的是:

structure *ptr = malloc(sizeof(ptr));

这分配了足够的空间来容纳ptr,这是一个指针,你需要

structure *ptr = malloc(sizeof(*ptr));

正确分配所有内容后,访问第一个元素中的内容,如下所示:

ptr->data; // the data
ptr->next; // pointer to the next struct in the chain

像这样访问第二个struct中的东西

ptr->next->data; // data in the second struct
ptr->next->next; // pointer to the third struct

等等。

刚刚阅读了有关该问题的一些 cmets,我应该补充一点,ptr-&gt;next-&gt;next 本质上是危险的,除非您知道ptrptr-&gt;next 都是非空的。此外,malloc 不保证内存归零,因此在调用malloc 后,您应始终确保next 指针为NULL

如果您有一个长链,并且最后一项由 NULL next 表示,您可以使用 for 循环很好地遍历链,例如

for (structure* current = ptr ; current != NULL ; current = current->next)
{
    printf("%d\n", current->data);
}

或者,如果你想找到nth 项

structure* ptrAtIndex(structure* start, int index) 
{
    for (structure* current = ptr, int i = 0 ; current != NULL ; current = current->next, i++)
    {
        if (i == index)
        {
            return current;
        }
    }
    return NULL; // The chain wasn't long enough
}

【讨论】:

  • 为了完整起见,您可能想要编辑您的答案,以解决 OP 的问题,即无需链接-&gt;next
  • @Srini 是的,只是想想
  • 感谢您指出(呵呵)我忘记了星号 :) 我的意思更像是,您是否会使用我显示的方式而不声明另一个指针或声明它。
  • @RollAndSchullz 查看我的示例以了解列表。我已经写过很多次这样的循环了。
猜你喜欢
  • 2021-01-15
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多