[英]write data to specific memory address issue
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.
函数写入的“参数1中的类型错误”。
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. 据我了解
S_LEN
是一个指向0x1814
,所以应该以正确的方式工作,但在这种情况下,我得到了以前的错误。
when i replace the command write(S_LEN,0);
当我替换命令
write(S_LEN,0);
via write(&S_LEN,0);
通过
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);
}
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: 您的
write
函数期望将 int32_t *
作为第一个参数,它是一个指针 ,因此,如果您希望S_LEN
作为指针 ,则应使用如下所示的内容:
#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). 由于在
int32_t *
(这是一个指针)上应用下标运算符 []
,因此表达式的计算结果为int32_t
。
On the other hand &S_LEN
is accepted, beause it yields the address of the int32_t
you get by applying []
. 另一方面,
&S_LEN
被接受,因为它产生通过应用[]
获得的int32_t
的地址。
Also note that S_LEN
relates to the memory location 4*0xFF+0x1800
which is 0x1BFC
. 另请注意,
S_LEN
与存储位置4*0xFF+0x1800
,即0x1BFC
。 (It is 255
int32_t
s after 0x1800
). (
0x1800
之后为255
int32_t
s)。
In your code, S_LEN
does the following: 在您的代码中,
S_LEN
执行以下操作:
RAM_START
to a pointer of type int32_t *
RAM_START
转换为int32_t *
类型的指针int32_t *
int32_t *
should be a int32_t
number int32_t *
的指针应为int32_t
But write
need first argument to be of type int32_t *
, so &S_LEN
is right, cause the address of an int32_t
returned by S_LEN
is of type int32_t *
但是
write
第一个参数必须是int32_t *
类型,所以&S_LEN
是正确的,因为S_LEN
返回的int32_t
的地址是int32_t *
类型
The output from OSX 10.11: OSX 10.11的输出:
[oxnz@rmbp:tmp:0]$ cc a.c -o a
a.c:14:8: warning: incompatible integer to pointer conversion passing 'int32_t'
(aka 'int') to parameter of type 'int32_t *' (aka 'int *'); take the
address with & [-Wint-conversion]
write(S_LEN,0);
^~~~~
&( )
a.c:5:16: note: expanded from macro 'S_LEN'
#define S_LEN (((int32_t *)(RAM_START))[0xFF])
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.c:6:24: note: passing argument to parameter 'dest_ptr' here
int32_t write(int32_t *dest_ptr, int32_t src)
^
1 warning generated.
[oxnz@rmbp:tmp:0]$ cat a.c
#include <stdio.h>
#include <stdlib.h>
#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;
}
int main()
{
write(S_LEN,0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.