在C中更改结构的地址

用户名

假设给了我一个结构,我需要将其所有属性分配给一个特定的地址。下面的代码给了我一个条件错误,但是我没有试图对其进行评估。

struct header block_o_data;
block_o_data.a = 1;
block_o_data.b = 2;
void* startingAddress = sbrk(0);
&block_o_data = *address;

请让我知道我做错了什么。

青蛙

假设您有一个这样的结构:

struct mystruct {
    int a;
    char b;
};

那么您可能需要这样的东西:

// A pointer variable supposed to point to an instance of the struct
struct mystruct *pointer;

// This is a general address represented by void*
void *addr = some_function(0);

// Cast that general address to a pointer varibale pointing to
// an instance of the struct
pointer = (struct mystruct *) addr;

// Use it!
printf("%d", pointer->a);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章