在C更改结构的地址 [英] Change address of struct in C

查看:98
本文介绍了在C更改结构的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我得到了一个结构,我需要所有它的属性分配给一个特定的地址。下面的code是给我一个有条件的错误,但我并不想对其进行评估。

Let's say that I was given a struct and I need to assign all of it's attributes to a particular address. The code below is giving me a conditional error, but i'm not trying to evaluate it.

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

请让我知道什么即时通讯做错了。

Please let me know what im doing wrong.

推荐答案

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

Suppose you have a struct like this:

struct mystruct {
    int a;
    char b;
};

,那么你可能需要的东西是这样的:

then you probably need something like this:

// 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);

这篇关于在C更改结构的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆