问题描述
我有一个问题,我有一个指向内存区域。我想利用这个指针来创建一个整数数组。
I have a problem where I have a pointer to an area in memory. I would like to use this pointer to create an integer array.
本质上讲,这是我,一个指针大小的内存地址100 * 300 * 2 = 60000字节
Essentially this is what I have, a pointer to a memory address of size 100*300*2 = 60000 bytes
unsigned char *ptr = 0x00000000; // fictional point in memory goes up to 0x0000EA60
我想实现的是考察这个内存大小的整型数组100 * 150 = 15000整数= 60000字节,是这样的:
What i would like to achieve is to examine this memory as an integer array of size 100*150 = 15000 ints = 60000 bytes, like this:
unsigned int array[ 100 ][ 150 ];
我假设它涉及到一些铸造虽然我不知道究竟是如何制定的。任何帮助将是AP preciated。
I'm assuming it involves some casting though i'm not sure exactly how to formulate it. Any help would be appreciated.
推荐答案
您可以将指针转换为无符号整数(*)[150]
。然后,它可以被用来如同这是一个二维数组(好像,因为sizeof的 的行为是不同的)。
You can cast the pointer to unsigned int (*)[150]
. It can then be used as if it is a 2D array ("as if", since behavior of sizeof
is different).
unsigned int (*array)[150] = (unsigned int (*)[150]) ptr;
这篇关于投无效指针指向整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!