简体   繁体   English

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

[英]How can I declare and unpack an array of pairs of void pointers?

I would like to write a function that takes an array of void* "pairs" - each one is a pair of pointers to something.我想写一个 function ,它需要一个void* “对”数组 - 每个都是一对指向某物的指针。 The first element in each pair will be a string, and the second will be a pointer to something else.每对中的第一个元素将是一个字符串,第二个元素将是指向其他内容的指针。

I would like the array passed into the function to be declared like so:我希望传递到 function 的数组声明如下:

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

function(argument);

Inside function , I would like to iterate over the array, access each "pair", cast the members of the "pair" to their respective types (known to me) and make use of these members.function内部,我想遍历数组,访问每个“对”,将“对”的成员转换为它们各自的类型(我知道)并利用这些成员。

I'm having some trouble with this.我遇到了一些麻烦。 At first, I tried declaring the argument as void* thing[][] .起初,我尝试将参数声明为void* thing[][] Thinking it's an array of arrays of void pointers.认为它是一个由void指针组成的 arrays 数组。

However, simply trying to access an index in such an array gives me the error: expression must be a pointer to a complete object type .但是,简单地尝试访问此类数组中的索引会给我错误: expression must be a pointer to a complete object type

Reading on SO I realized the compiler is thinking I'm trying to dereference a void* .阅读 SO 我意识到编译器正在考虑我正在尝试取消引用void* But I thought that doing a[0] on such an array would access the first "pair" in the array, which should be dereferencing a void** - which is legal.但我认为在这样的数组上执行a[0]将访问数组中的第一个“对”,这应该取消引用void** - 这是合法的。 Am I wrong?我错了吗?

What is the correct approach to go about this, in terms of the type of the array, it's initialization and the way to unpack the array during iteration?关于这个问题,go 的正确方法是什么,就数组的类型、它的初始化和迭代期间解包数组的方式而言?

You want void *argument[][2] = { /*... */ };你想要void *argument[][2] = { /*... */ }; . . You have an array of unspecified size, whose elements are arrays of fixed size 2, whose elements are pointers to void .您有一个未指定大小的数组,其元素是固定大小为 2 的 arrays,其元素是指向void的指针。 Your original compiler error was because when you have multiple square braces, only the first set of them can be empty, so adding the 2 fixed it.您最初的编译器错误是因为当您有多个方括号时,只有第一组可以为空,因此添加2修复了它。

By the way, it can decay as a pointer, in which case it would be void *(*argument)[2] .顺便说一句,它可以作为指针衰减,在这种情况下它将是void *(*argument)[2]

Note that your sentinel NULL doesn't do quite what you'd expect, since it's in an array of arrays, and arrays aren't quite pointers.请注意,您的哨兵NULL并没有达到您的预期,因为它位于 arrays 和 arrays 的数组中,并不是完全的指针。 Note this warning:请注意此警告:

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

Here's a sample program that uses the type:这是一个使用该类型的示例程序:

#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;
}

As a final note, I'd say your life would probably be simpler with a custom struct :最后一点,我想说使用自定义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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM