简体   繁体   English

在C99中的结构中将指针分配给2D空数组

[英]Assign a pointer to a 2D void array in a struct in C99

I have a 2d buffer that I would like to pass to a thread along with other values so I need to move it into a struct. 我有一个2d缓冲区,我想将其与其他值一起传递给线程,因此我需要将其移至结构中。 When I try to do this, I cannot access the values in again in my struct and I need some help. 当我尝试执行此操作时,无法在结构中再次访问这些值,并且需要一些帮助。

Here's my buffer: 这是我的缓冲区:

void* mybuff[8][16384];

I would like to store a pointer to this 2D array in a struct like this: 我想在这样的结构中存储指向此2D数组的指针:

typedef struct  {
  int i;
  void *mybuff;
} arg_struct_t;

However if I try to access the values: 但是,如果我尝试访问这些值:

    arg_struct_t *args = malloc(sizeof(arg_struct_t));
    args->i = index;
    args->mybuff = mybuff;

    fprintf(stderr, "%p\n", args->mybuff[0][0]);

I get an error: 我收到一个错误:

main.c:104:47: warning: dereferencing ‘void *’ pointer [enabled by default]
           fprintf(stderr, "%p\n", args->mybuff[0][0]);
                                               ^
main.c:104:50: error: subscripted value is neither array nor pointer nor vector
           fprintf(stderr, "%p\n", args->mybuff[0][0]);
                                                  ^

I think I need to tell C99 more about the shape of the data I want to put into my struct, but I can't quite figure out the right way to do that. 我想我需要告诉C99更多有关要放入结构中的数据形状的信息,但是我还不太想办法做到这一点。 I appreciate any help or insights here. 感谢您在此提供的任何帮助或见解。

The compiler doesn't know, what address in the memory it must calculate here: 编译器不知道,它必须在此处计算内存中的哪个地址:

args->mybuff[0][0]

because args->mybuff is just a pointer to void. 因为args->mybuff只是指向void的指针。 To process a such expression the compiler needs info about 1) size of the elements of the array and 2) at least one of the dimensions of the array. 为了处理这样的表达式,编译器需要以下信息:1)数组元素的大小和2)数组尺寸中的至少一个。 Following code is more correct: 以下代码更正确:

typedef void* TBuff[8][16834];
TBuff mybuff;

typedef struct  {
  int i;
  TBuff * mybuff;
} arg_struct_t;

arg_struct_t *args = malloc(sizeof(arg_struct_t));
args->i = index;
args->mybuff = &mybuff;
fprintf(stderr, "%p\n", (*args->mybuff)[0][0]);

At the risk of getting you farther over your head: 冒着将您拉到更远的危险:

typedef struct  {
  int i;
  void* mybuff[8][16384];
} arg_struct_t;

But the general nature of void* is generic typing which is contrary to multidimensional arrays. 但是void *的一般性质是通用类型,与多维数组相反。 This code looks strange as soon as that type appears and there's probably something else wrong. 出现该类型后,此代码看起来很奇怪,并且可能还有其他错误。

Here is how to store the address of mybuff, and how to use that pointer later to access values. 这是如何存储mybuff的地址,以及以后如何使用该指针访问值的方法。

int main()
{
    // Starting array    
    int* mybuff[8][16384];

    // Save the address of mybuff into 'p'. 'p' is a pointer to (int*)'s
    int** p = &mybuff[0][0];

    // Access the location of row=3, col=4 using 'p'
    int r = 3;
    int c = 4;
    *(p + (r*16384 + c)) = (int*) 123; // 123 is the value to assign

    return 0;
}

So you'll also need to pass the row and column sizes (8 and 16384) in your struct too in order to dereference the values. 因此,您还需要在结构中传递行和列的大小(8和16384),以便取消引用值。 I've left out bounds checking in the example for brevity, but you should do that in real code. 为了简洁起见,我在示例中没有进行边界检查,但是您应该在实际代码中进行检查。

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

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