我如何声明和解压缩成对的空指针数组?

问题描述 投票:1回答:1

我想编写一个函数,该函数采用void*个“对”的数组-每个函数都是一对指向某物的指针。每对中的第一个元素将是一个字符串,第二个元素将是指向其他内容的指针。

我希望传递给函数的数组像这样声明:

<what type?> argument = {
        { "some string", <some pointer>},
        { "some other string", <some other pointer>},
        // ... etc
        NULL // To signal ending
}

function(argument);

function内,我要遍历数组,访问每个“对”,将“对”的成员转换为各自的类型(对我来说是已知的,并使用这些成员。

我对此有些麻烦。首先,我尝试将参数声明为void* thing[][]。认为它是void指针数组的数组。

但是,仅尝试访问此类数组中的索引会给我错误:expression must be a pointer to a complete object type

继续阅读,我意识到编译器正在考虑我要取消引用void*。但是我认为在这样的数组上执行a[0]将访问数组中的第一个“对”,这应该取消引用void**-这是合法的。我错了吗?

关于数组的类型,初始化和在迭代过程中解压缩数组的正确方法是什么?

c arrays void-pointers
1个回答
3
投票

您想要void *argument[][2] = { /* ... */ };。您有一个未指定大小的数组,其元素是固定大小2的数组,其元素是void的指针。您最初的编译器错误是因为当您有多个方括号时,只有第一组方括号可以为空,因此添加2可以将其固定。

顺便说一下,它可以作为指针衰减,在这种情况下,它将是void *(*argument)[2]

请注意,前哨NULL并没有达到您的期望,因为它位于数组数组中,而数组也不是指针。注意此警告:

warning: suggest braces around initialization of subobject [-Wmissing-braces]
        NULL // To signal ending
        ^~~~
        {   }

这里是使用类型的示例程序:

#include <stdio.h>

void *g[][2] = {
        { "some string", (void*)0x1234},
        { "some other string", (void*)0x5678},
        { "some third string", (void*)0x9abc},
        // ... etc
        {NULL} // To signal ending
};


void print_things(void *argument[][2]) {
    for(int i = 0; argument[i][0] /* using just argument[i] instead of argument[i][0] here would not work as intended */; ++i) {
        printf("%s %p\n", (char*)argument[i][0], argument[i][1]);
    }
}

int main(void) {
    print_things(g);
    return 0;
}

最后,我想说,使用自定义struct,您的生活可能会更简单:

#include <stdio.h>

struct pair {
    char *str;
    void *ptr;
} g[] = {
        { "some string", (void*)0x1234},
        { "some other string", (void*)0x5678},
        { "some third string", (void*)0x9abc},
        // ... etc
        {NULL, NULL} // To signal ending
};


void print_things(struct pair argument[]) {
    for(int i = 0; argument[i].str; ++i) {
        printf("%s %p\n", argument[i].str, argument[i].ptr);
    }
}

int main(void) {
    print_things(g);
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.