当我使用以下代码在内存的特定地址写入数据时,我得到一个错误告诉我
函数写入的“参数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).
更多推荐
发布评论