将数据写入特定的内存地址问题(write data to specific memory address issue)

编程入门 行业动态 更新时间:2024-10-27 02:29:37
将数据写入特定的内存地址问题(write data to specific memory address issue)

当我使用以下代码在内存的特定地址写入数据时,我得到一个错误告诉我

函数写入的“参数1中的类型错误”。

据我所知, S_LEN是一个指向S_LEN的指针所以它应该以正确的方式工作,但在这种情况下我得到了前一个错误。

当我替换命令write(S_LEN,0); 通过write(&S_LEN,0); 它的工作正确。 虽然在第一种情况下我将十六进制数转换为指向整数的指针。

任何人都可以解释这件事吗?

#define RAM_START 0x1800 #define S_LEN (((int32_t *)(RAM_START))[0xFF]) int32_t write(int32_t *dest_ptr, int32_t src) { *dest_ptr = src; return 0; } main() { write(S_LEN,0); }

When I use the following code to write data at specific address at memory i get an error told me that

" type error in argument 1" of function write.

as i understand S_LEN is a pointer to 0x1814 so it should work in correct way but at this case i get the previous error.

when i replace the command write(S_LEN,0); via write(&S_LEN,0); it work correct. although at first case i pass hex number casting to pointer to integer.

can any one explain this matter?

#define RAM_START 0x1800 #define S_LEN (((int32_t *)(RAM_START))[0xFF]) int32_t write(int32_t *dest_ptr, int32_t src) { *dest_ptr = src; return 0; } main() { write(S_LEN,0); }

最满意答案

你的write函数需要 int32_t *作为第一个参数,它是一个指针 ,所以如果你想让S_LEN成为一个指针,你应该使用类似这样的东西:

#define S_LEN (((int32_t *)(RAM_START))+0xFF)

由于您在int32_t * (它是一个指针)上应用下标运算符 [] ,因此您的表达式求值为int32_t 。

另一方面&S_LEN被接受,因为它通过应用[]产生你得到的int32_t的地址。

还要注意, S_LEN与存储单元4*0xFF+0x1800 ,即0x1BFC 。 (在0x1800之后是255 int32_t s)。

Your write function expects int32_t * as first argument, which is a pointer, so you should use something like this instead, if you want S_LEN to be a pointer:

#define S_LEN (((int32_t *)(RAM_START))+0xFF)

Your expression evaluates to an int32_t since you apply the subscript operator [] on the int32_t * (which is a pointer).

On the other hand &S_LEN is accepted, beause it yields the address of the int32_t you get by applying [].

Also note that S_LEN relates to the memory location 4*0xFF+0x1800 which is 0x1BFC. (It is 255 int32_ts after 0x1800).

更多推荐

本文发布于:2023-07-24 01:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   地址   数据   write   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!