分配和访问结构体中字符串的指针

发布于 2024-11-03 03:35:30 字数 1338 浏览 4 评论 0原文

我试图将字符串存储在结构中包含的数组中,并访问它,但我遇到了困难。该结构如下所示:

typedef struct {
    void **storage;
    int numStorage;
} Box;

Box 初始化如下:

    b->numStorage = 1000000; // Or set more intelligently
    Box *b = malloc(sizeof(Box));
    // Create an array of pointers
    b->storage = calloc(b->numStorage,sizeof(void *));

为了设置字符串,我使用此函数:

void SetString(Box *b, int offset, const char * key)
{
    // This may seem redundant but is necessary
    // I know I could do strcpy, but made the following alternate
    // this isn't the issue
    char * keyValue = malloc(strlen(key) + 1);
    memcpy(keyValue, key, strlen(key) + 1);

    // Assign keyValue to the offset pointer
    b->storage[offset*sizeof(void *)] = &keyValue;

    // Check if it works
    char ** ptr = b->storage[offset*sizeof(void *)];

    // It does
    printf("Hashcode %d, data contained %s\n", offset, *ptr);

}

问题在于,当我尝试使用完全相同的偏移量再次检索它时:

// Return pointer to string
void *GetString(const Box *b, int offset, const char *key)

    char ** ptr = b->storage[offset*sizeof(void *)];
    if (ptr != NULL) {
        printf("Data should be %s\n", *ptr);
        return *ptr;
    } else {
     return NULL;
    }

返回的指针是乱码。可能出什么问题了?

I'm trying to store a string in an array contained within a struct, and access it, but I'm having a hard time. The struct looks like this:

typedef struct {
    void **storage;
    int numStorage;
} Box;

Box is initialized as such:

    b->numStorage = 1000000; // Or set more intelligently
    Box *b = malloc(sizeof(Box));
    // Create an array of pointers
    b->storage = calloc(b->numStorage,sizeof(void *));

In order to set the string, I use this function:

void SetString(Box *b, int offset, const char * key)
{
    // This may seem redundant but is necessary
    // I know I could do strcpy, but made the following alternate
    // this isn't the issue
    char * keyValue = malloc(strlen(key) + 1);
    memcpy(keyValue, key, strlen(key) + 1);

    // Assign keyValue to the offset pointer
    b->storage[offset*sizeof(void *)] = &keyValue;

    // Check if it works
    char ** ptr = b->storage[offset*sizeof(void *)];

    // It does
    printf("Hashcode %d, data contained %s\n", offset, *ptr);

}

The problem lies when I try to retrieve it again, with the exact same offset:

// Return pointer to string
void *GetString(const Box *b, int offset, const char *key)

    char ** ptr = b->storage[offset*sizeof(void *)];
    if (ptr != NULL) {
        printf("Data should be %s\n", *ptr);
        return *ptr;
    } else {
     return NULL;
    }

The returned pointer is gibberish. What could be amiss?

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

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

发布评论

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

评论(3

溺孤伤于心 2024-11-10 03:35:30

访问数组时不必指定实际的内存偏移量。只需给它索引,您就会得到正确的元素。

那么,在你的第三个代码块中:

b->storage[offset] = keyValue;

在你的第四个代码块中:

char *ptr = b->storage[offset];
if (ptr != NULL) {
    printf("Data should be %s\n", ptr);
    return ptr;
} else {
 return NULL;
}

另外,在第二个代码块中,是否已设置 b->numStorage

You don't have to specify the actual memory offset when accessing arrays. Simply give it the index and you will get the correct element.

So, in your third code block:

b->storage[offset] = keyValue;

And in your fourth:

char *ptr = b->storage[offset];
if (ptr != NULL) {
    printf("Data should be %s\n", ptr);
    return ptr;
} else {
 return NULL;
}

Also, in the second block of code, has b->numStorage already been set?

南…巷孤猫 2024-11-10 03:35:30
b->storage[offset*sizeof(void *)] = &keyValue;

这将局部变量keyValue的地址存储在数组中。一旦函数完成,该地址就变得无效。我想你想要:

b->storage[offset*sizeof(void *)] = keyValue;

然后在检索时进行相应的更改。

b->storage[offset*sizeof(void *)] = &keyValue;

This stores the address of the local variable keyValue in the array. Once the function completes, this address becomes invalid. I think you want:

b->storage[offset*sizeof(void *)] = keyValue;

and then make the corresponding change while retrieving.

云淡月浅 2024-11-10 03:35:30

这不就是:

b->storage[offset*sizeof(void *)] = &keyValue

设置storage[offset*sizeof(void*)]指向局部变量keyValue的地址吗?即函数返回后不再有效

Doesn't this:

b->storage[offset*sizeof(void *)] = &keyValue

set storage[offset*sizeof(void*)] to point to the address of the local variable keyValue? i.e. no longer valid after function returns

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