简体   繁体   English

如何在结构数组中获取结构地址?

[英]How to get struct address inside array of structs?

I'm trying to get struct's address.我正在尝试获取结构的地址。

I want to get address in an int * , and I want to change address by adding numbers to the int * .我想在int *中获取地址,并且我想通过向int *添加数字来更改地址。 I tried several ways, but I can't solve it.我尝试了几种方法,但我无法解决。

struct num_d {
    unsigned char data;    
    unsigned char pad1;
    unsigned char pad2;
    unsigned char pad3;
};
struct num_d **m = malloc(sizeof(struct num_d *) * row);   

for (int i = 0; i < row; i++)                       
{                                                   
    m[i] = malloc(sizeof(struct num_d) * col);    
}

How can I get m[0][0] 's address in an int * ?如何在int *中获取m[0][0]的地址?

first things first lets typedef your struct, so we can type less and be more clear:首先让typedef你的结构,所以我们可以输入更少并且更清晰:

typedef struct num_d num_d;

void pointer空指针

A pointer to void is a "generic" pointer type.指向 void 的指针是“通用”指针类型。 A void * can be converted to any other pointer type without an explicit cast . void *无需显式强制转换即可转换为任何其他指针类型。 we cannot de-reference a void * or do pointer arithmetic with it;我们不能取消引用void *或用它做指针运算; you must convert it to a complete data type pointer first (like int* eg) then do the de-refrence or the pointer arithmetic.您必须先将其转换为完整的数据类型指针(例如int* eg),然后再进行取消引用或指针运算。

Now, malloc() return a void* which points to the allocated heap buffer (if malloc successed in allocation other wise null is the return value).现在, malloc()返回一个指向分配的堆缓冲区的void* (如果 malloc 分配成功,否则 null 是返回值)。

you code become:你的代码变成:

num_d** m = malloc(sizeof(num_d*) * row);  /*m is an array of void* pointers (not initialized)*/ 

for (int i = 0; i < row; i++)                       
{                                                   
    m[i] = malloc(sizeof(num_d) * col); /*in each element in m you have a void* that points to struct num_d on the heap*/    
}

the sizeof(void*) is the same as sizeof any pointer (except function pointers in some machines/os). sizeof(void*)与任何指针的 sizeof 相同(某些机器/操作系统中的 function 指针除外)。

putting it all together把它们放在一起

How can I get m[0][0]'s address in an int *?如何在 int * 中获取 m[0][0] 的地址?

This is a wrong question!这是一个错误的问题! because m is an array of void* to "num_d structs" (holding the num_d heap address).因为 m 是void*到“num_d structs”(保存 num_d 堆地址)的数组。

if you want the start address of the i-th num_d struct in the array m , then, just return the void* in the index i in this array m[i] .如果您想要数组mi-th num_d结构的起始地址,则只需返回此数组m[i]中索引i中的void* and if you want to cast it just cast it (no need actually) just assign it:如果你想投射它,只需投射它(实际上不需要)只需分配它:

int* ptr = m[i];

Take in mind that compilers will warn you, regarding the assignment above (but this assignment is supported and legal):请记住,编译器会警告您,关于上述分配(但此分配是受支持且合法的):

warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]

or (no need again):或(不再需要):

int* ptr = (int*)m[i];

I don't know why you need such behavior, it makes more sense to cast to num_d*我不知道你为什么需要这种行为,强制转换为num_d*更有意义

if you want the address of the first data member in the struct num_d , then you must cast to the appropriate data type to get the expected data:如果您想要 struct num_d中第一个数据成员的地址,则必须强制转换为适当的数据类型以获取预期数据:

unsigned char data = ((num_d*)m[i])->data;

unsigned char* p_data = &((num_d*)m[i])->data;

You don't need to have the address in an int* in order to be adding to it.您无需将地址放在int*中即可添加。 The way that [] works, is that it adds to the pointer and dereferences. []的工作方式是它添加到指针和取消引用。

You can just add to *(m[0] + 1) to get the second element.您只需添加到*(m[0] + 1)即可获得第二个元素。

How about:怎么样:

int *ptr = (int *) m[0]; int *ptr = (int *) m[0];

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

相关问题 如何获取结构内部的结构并将其分配给结构数组 - how to get a struct that inside of a struct and assign it to an array of structs 释放结构内部的结构数组 - Freeing array of structs inside struct 如何通过引用修改指向另一个结构内部的结构数组的指针 - How to modify pointer to array of structs inside another struct by reference 如何在C中访问结构数组中的结构成员 - how to access struct member inside an array of structs in C 如何在结构数组中访问结构 - How to access struct in an array of structs 如何为结构,结构内部的结构数组正确分配内存,以及如何将该数组作为参数传递 - How to properly allocate memory for structs, arrays of structs inside a struct, and passing that array as a parameter 如何初始化结构内部的指向NULL的指针数组,并且该结构位于其他结构的数组中 - How to initialise an array of pointers to NULL that's inside a struct, and the struct is within an array of other structs 另一个Struct内部的嵌套结构数组 - Nested Array of Structs inside another Struct 结构内部的数组数组存在段故障问题 - Segfault Issues With Array of Structs Inside a Struct 一个带有一个结构数组的结构,里面有一个数组(和一个数组)里面它:我怎么用malloc呢? - A Struct with an Array of Structs with Arrays inside Them (and an Array) inside It: How would I malloc this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM