[英]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]
.如果您想要数组
m
中i-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.