[英]correct way to initialize a multidimensional array pointer from a particular address
I want to create a multidimensional array pointer in C with a particular starting address in memory. 我想在C中使用内存中的特定起始地址创建多维数组指针。 I don't fully understand the correct syntax for this. 我不完全了解正确的语法。 What i want to accomplish to do is something like follows: 我要完成的工作如下:
unsigned char (*vptr)[240][320] = (unsigned char*)0x40000000;
where 哪里
vptr[0][0] = 2; vptr [0] [0] = 2; would write 2 to address 0x40000000 ; 将2写入地址0x40000000 ;
vptr[0][1] = 3; vptr [0] [1] = 3; would write 3 to address 0x40000001 ; 将3写入地址0x40000001 ;
... ...
vptr[1][0] = 4; vptr [1] [0] = 4; would write 4 to address 0x40000240 ; 将4写入地址0x40000240 ;
So basically that my vptr would be functionally equivalent to 基本上,我的vptr在功能上等同于
unsigned char vptr[240][320] = {....};
where &vptr == 0x40000000 . 其中&vptr == 0x40000000 。
Could someone please write how this is supposed to be done? 有人可以写出应该怎么做吗? Thank you very much for the help. 非常感谢你的帮助。
You could cast the address as (void *)0x40000000
as that can be converted to any object type. 您可以将地址转换为(void *)0x40000000
,因为可以将其转换为任何对象类型。 Some compilers might issue a warning unless you cast the address as (void *)(uintptr_t)0x40000000
. 除非您将地址(void *)(uintptr_t)0x40000000
为(void *)(uintptr_t)0x40000000
否则某些编译器可能会发出警告。
With unsigned char (*vptr)[240][320]
, you would need to access the individual unsigned char
elements using (*vptr)[i][j]
. 使用unsigned char (*vptr)[240][320]
,您将需要使用(*vptr)[i][j]
访问各个unsigned char
元素。 Therefore, to make things easier, it would be better to declare vptr
as unsigned char (*vptr)[320]
so that vptr
is a pointer to the first element of an array of char[320]
. 因此,为了使事情变得容易,最好将vptr
声明为unsigned char (*vptr)[320]
以便vptr
是指向char[320]
数组的第一个元素的指针。 Then you can access the individual unsigned char
elements using the more natural syntax vptr[i][j]
. 然后,您可以使用更自然的语法vptr[i][j]
访问各个unsigned char
元素。 For example vptr[10][20]
would access the unsigned char
element at address 0x40000000 + (10 * 320) + 20
( 0x40000c94
), or in general, vptr[i][j]
would access the unsigned char
element at address 0x40000000 + (i * 320) + j
. 例如vptr[10][20]
将访问unsigned char
在地址元件0x40000000 + (10 * 320) + 20
( 0x40000c94
),或者在一般情况下, vptr[i][j]
将访问unsigned char
在地址元件0x40000000 + (i * 320) + j
。
If you don't want to cast the address to (void *)
, you can cast to the correct pointer type instead. 如果不想将地址强制转换为(void *)
,则可以强制转换为正确的指针类型。 Eg unsigned char (*vptr)[240][320] = (unsigned char (*)[240][320])0x40000000;
例如unsigned char (*vptr)[240][320] = (unsigned char (*)[240][320])0x40000000;
or unsigned char (*vptr)[320] = (unsigned char (*)[320])0x40000000;
或unsigned char (*vptr)[320] = (unsigned char (*)[320])0x40000000;
. 。 (You might still need the intermediate (uintptr_t)
cast to avoid a compiler warning.) (您可能仍需要中间(uintptr_t)
以避免出现编译器警告。)
You mentioned that &vptr == 0x40000000
(disregarding casting), but that is not the case. 您提到了&vptr == 0x40000000
(不考虑强制转换),但是事实并非如此。 vptr
is just a pointer variable at whatever address the compiler and linker decide to place it. vptr
只是指针变量,无论编译器和链接器决定将其放置在任何地址。 It is the vptr
variable itself (not its address) that has the value 0x40000000
(disregarding casting). vptr
变量本身(而不是其地址)的值是0x40000000
(不考虑转换)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.