简体   繁体   English

C:memory 地址数组?

[英]C: Array of memory addresses?

I'm trying to make a sort of container for multiple different structs.我正在尝试为多个不同的结构制作一种容器。 Unfortunately C only allows type specific arrays, meaning I'd have to make a different array for each type of struct.不幸的是 C 只允许特定类型的 arrays,这意味着我必须为每种类型的结构创建一个不同的数组。

The current solution I came up with is a container that holds memory addresses.我想出的当前解决方案是一个包含 memory 地址的容器。 This way the program can just pass the memory address of one of the elements to a function.这样,程序就可以将其中一个元素的 memory 地址传递给 function。

Currently the only code I have is a failed attempt using void pointers (not really familiar with pointers and memory addresses yet unfortunately)目前我唯一的代码是使用 void 指针的失败尝试(不幸的是,对指针和 memory 地址不太熟悉)

The following is my test code I was writing to try and understand how this stuff works:以下是我正在编写的测试代码,以尝试了解这些东西是如何工作的:

void* arr[10]={};
int len=0;

int n[5]={1,2,3,4,5};
for (int i=0;i<5;i++) { //add pointers nums in n to arr
  arr[i]=(void*)(&n[i]);
  len++;
}

for (int i=0;i<len;i++) { //print contents of arr
  printf("%p\n", (void*)arr[i]);
  printf("\t%d\n", arr[i]); //trying to print actual value (eg. 2 for index 2) but not really sure how to...
}

Thanks!谢谢!

You need to derefence the pointer stored in the array.您需要取消引用存储在数组中的指针。 You also need to cast it to the original type of the referenced objects.您还需要将其转换为引用对象的原始类型。

    printf("\t%d\n", *(int *)arr[i]); 

Your approach is correct but there is some stuff missing...您的方法是正确的,但是缺少一些东西...

In C any object pointer can be converted to a void-pointer and back to a pointer of the original type.在 C 中,任何 object 指针都可以转换为 void 指针并返回到原始类型的指针。 So an int-pointer can be converted to a void-pointer an back to an int-pointer.因此,一个 int-pointer 可以转换为一个 void-pointer 并返回一个 int-pointer。 And a float-pointer can be converted to a void-pointer an back to an float-pointer.并且可以将浮点指针转换为 void 指针并返回到浮点指针。

So using an array of void-pointers to store pointers to different object types is a fine approach.因此,使用 void 指针数组来存储指向不同 object 类型的指针是一种很好的方法。

But... in order to convert the void-pointer back to the original type, you need to know what the original type was.但是...为了将 void-pointer 转换回原始类型,您需要知道原始类型是什么。 If you just saves the void-pointer, you don't have that information.如果您只是保存 void-pointer,则您没有该信息。

Instead consider something like:而是考虑类似的事情:

struct gp
{
    void* p;
    unsigned type_tag;
}

#define INT_TYPE 0
#define FLOAT_TYPE 1

and use it like:并像这样使用它:

struct gp arr[2];
int n = 42;
float f = 42.42;

arr[0].p = &n;
arr[0].type_tag = INT_TYPE;

arr[1].p = &f;
arr[1].type_tag = FLOAT_TYPE;

for (int i=0; i < 2; ++i)
{
    if (arr[i].type_tag == INT_TYPE)
    {
        int* p = (int*)arr[i].p;  // Cast void-pointer back to int-pointer
        printf("%d\n", *p);       // Get int-value using *p, i.e. dereference the pointer
    }
    else if (arr[i].type_tag == FLOAT_TYPE)
    {
        int* p = (float*)arr[i].p;  // Cast void-pointer back to float-pointer
        printf("%f\n", *p);         // Get float-value using *p, i.e. dereference the pointer
    }
}

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

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