如何使用地址赋值?

发布于 2024-11-04 09:13:49 字数 180 浏览 0 评论 0原文

我在C语言面试时被问到一个问题。 问题是:

int *point;
'0x983234' is a address of memory;//I can not remember exactly

我们如何将 20 分配给该内存? 这看起来像是一个嵌入式编程问题,有人能解释一下吗?

I was asked a question during an C Language interview.
the question is:

int *point;
'0x983234' is a address of memory;//I can not remember exactly

how could we assign 20 to that memory?
it looks likes a embedded programming question, can anyone explains me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在夏天 2024-11-11 09:13:49

我怀疑这是一个嵌入式编程问题,你记错了。这可能是这样的——

int *point = (int *) 0x983234;
*point = 20;

当嵌入式程序员想要在地址 0x983234 处读/写一个寄存器时,他们就会做类似的事情。

I suspect that it is an embedded programming question, and that you are misremembering it slightly. It was probably something like-

int *point = (int *) 0x983234;
*point = 20;

Embedded programmers do do stuff like that when there is a register that they want to read/write at address 0x983234.

酸甜透明夹心 2024-11-11 09:13:49

首先,您必须将指针设置为正确的地址(以便它指向您需要的位置)。
然后,要在该地址写入,请取消引用指针并进行赋值。它看起来像这样:

int main ()
{
        volatile int *point = (int *)0x983234;
        *point = 20;
        return 0;
}

请注意 volatile 关键字。建议使用它,这样编译器就不会做任何假设并优化它。

如果您有更大的数据块要存储,请使用 memcpymemmove 与该地址从/向其复制数据,如下所示:

#include <string.h>

int main ()
{
        const char data[] = "some useful stuff";
        memcpy ((char *)0x983234, data, sizeof (data));
        return 0;
}

First you have to set your pointer to the right address (so that it points where you need it to).
Then, to write at that address, you dereference the pointer and do assignment. It will look something like this:

int main ()
{
        volatile int *point = (int *)0x983234;
        *point = 20;
        return 0;
}

Please note volatile keyword. It is recommended to use it so that compiler doesn't make any assumptions and optimize it.

If you have larger chunk of data to store, use memcpy or memmove with that address to copy data from/to it, like this:

#include <string.h>

int main ()
{
        const char data[] = "some useful stuff";
        memcpy ((char *)0x983234, data, sizeof (data));
        return 0;
}
末骤雨初歇 2024-11-11 09:13:49

我使用这种方法来做到这一点。它的好处是你不需要另一个变量(指针)。
假设您想将地址 0x00002dbd 处的 uint8_t 类型变量设置为 200。您可以简单地编写代码:

*(uint8_t*)0x00002dbd=200;

或者如果您有包含 uint8_t 变量地址的 uint32_t 变量(不是指针),您可以这样做

uint32_t x;
x=0x00002dbd;    
*(uint8_t*)x=200;

:你的目标变量是什么类型并不重要。只是类型而不是 uint8_t 部分。

I use this approach to do it.its benefit is that you don't need another variable (pointer).
assume that you want to set a variable of type uint8_t at adress 0x00002dbd to 200. you could code simply:

*(uint8_t*)0x00002dbd=200;

or if you a have uint32_t variable (not a pointer) that contains the address of the your uint8_t variable, you can do this:

uint32_t x;
x=0x00002dbd;    
*(uint8_t*)x=200;

it doesn't matter what type your target variable is. just the type instead of uint8_t part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文