访问 void * 结构

发布于 2024-11-03 05:14:36 字数 823 浏览 1 评论 0原文

我试图在这里找到我的实现中的错误,我将一个结构存储在另一个结构中,并且似乎无法访问存储的值。我定义了两个结构体。

typedef struct {
    void * data;

} Element;

typedef struct {
    void **buckets;
} Storage;

在一个单独的函数中,我将 key 设置为指向 char 的指针。并将其传入存储在数据中。

void Function(const char *key, Storage *storageUnit)
{
    char keyValue[strlen(key) + 1];
    strcpy(keyValue, key); // To fix the discard qualifiers bit
    Element data = { keyValue }; // = new struct element;
    printf("Key %s\n", (char *)data.data); // This works.
    *(storageUnit->buckets) = &data;

    // Let's see if it got stored correctly?
    Element temp = *(Element *)(storageUnit->buckets);

    // This is gobbledygook
    printf("Stored correctly with data %s", (char *)(temp.data));
}

我可能会错过什么?

I'm trying to find the bug in my implementation here, where I store a struct in another struct and cannot seem to access the value stored. I define two structs.

typedef struct {
    void * data;

} Element;

typedef struct {
    void **buckets;
} Storage;

In a separate function, I set key to be a pointer to a char. And pass it in to be stored in data.

void Function(const char *key, Storage *storageUnit)
{
    char keyValue[strlen(key) + 1];
    strcpy(keyValue, key); // To fix the discard qualifiers bit
    Element data = { keyValue }; // = new struct element;
    printf("Key %s\n", (char *)data.data); // This works.
    *(storageUnit->buckets) = &data;

    // Let's see if it got stored correctly?
    Element temp = *(Element *)(storageUnit->buckets);

    // This is gobbledygook
    printf("Stored correctly with data %s", (char *)(temp.data));
}

What could I be missing?

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

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

发布评论

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

评论(3

紅太極 2024-11-10 05:14:36

更改

Element temp = *(Element *)(storageUnit->buckets);

Element *temp = (Element *)*(storageUnit->buckets);

并打印语句为

printf("Stored correctly with data %s", (char *)(temp.data));

printf("Stored correctly with data %s", (char *)(temp->data));

编辑
不确定这是否是一个好方法,但我很确定您可以更改

Element temp = *(Element *)(storageUnit->buckets);

Element temp = *(Element *)*(storageUnit->buckets);

不更改打印语句并获得相同的行为。

change

Element temp = *(Element *)(storageUnit->buckets);

to

Element *temp = (Element *)*(storageUnit->buckets);

and print statement from

printf("Stored correctly with data %s", (char *)(temp.data));

to

printf("Stored correctly with data %s", (char *)(temp->data));

EDIT
Not sure if this is a good way to do it, but I am pretty sure you can change

Element temp = *(Element *)(storageUnit->buckets);

to

Element temp = *(Element *)*(storageUnit->buckets);

Without changing the print statement and get the same behavior.

潇烟暮雨 2024-11-10 05:14:36

我想也许你巩固了你的帖子?某一时刻它是 storageUnit->buckets,然后是 cm->buckets。这让我认为后面的打印实际上是在函数之外,在这种情况下它会爆炸,因为 Element data 是一个局部变量,一旦 Function() 返回,它就会消失。您已将自动装置添加到容器中。

我想你想要
元素数据 = new Element();
数据.数据=键值;

I think maybe you consolidated your posting a bit? at one point it's storageUnit->buckets, then it's cm->buckets. This makes me think the latter printing is actually outside the function, in which case it will blow up because Element data is a local variable that goes away once Function() returns. You've added an automatic to your container.

I think you want
Element data = new Element();
data.data = keyValue;

天赋异禀 2024-11-10 05:14:36

char keyValue[strlen(key)]; 是错误的,因为您使用 strcpy,您必须考虑 nul 终止符。将其设为char keyValue[strlen(key) + 1];。这可能是导致最后一个 printf 出现问题的原因。

*(storageUnit->buckets) = &data; 也可能是错误的。 datakeyValue 都在堆栈上分配。当您的 Function 返回时,这些对象不再有效,因此将该指针存储在 storageUnit->buckets[0] 中是没有用的。您可能应该使用例如 malloc() 动态分配元素和键。

char keyValue[strlen(key)]; is wrong since you use strcpy, you have to account for the nul terminator. make it char keyValue[strlen(key) + 1];. This is likely what's causing your issue in the last printf.

*(storageUnit->buckets) = &data; might be wrong too. data and keyValue are both allocated on the stack. When your Function returns, these objects are no longer valid, so storing that pointer in storageUnit->buckets[0] is useless. You should probably allocate the element and the key dynamically with e.g. malloc().

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