c - 访问内存地址数组

标签 c arrays pointers

我一直在用 C 语言编写简单的东西,现在我发现了一些更复杂的东西。

这个 PVSNESLIB 程序从 asm 中定义的图像中获取图案、调色板和贴图名称:

patterns:
.incbin "RES/images/bkg1.pic" 
patterns_end:
patterns1:
.incbin "RES/images/bkg2.pic" 
patterns1_end:
palette:
.incbin "RES/images/bkg1.pal"
palette_end:
palette1:
.incbin "RES/images/bkg2.pal"
palette1_end:
map:
.incbin "RES/images/bkg1.map"
map_end:
map1:
.incbin "RES/images/bkg2.map"
map1_end:
;;; and so on...

并使用这些示例函数在屏幕上显示它们:

bgInitTileSet(0, &patterns, &palette, 0, (&patterns_end - &patterns), 256*2, BG_256COLORS, 0x0000);
bgInitMapSet(0, &map, (&map_end - &map),SC_32x32, 0x2000);
   v++; 
//&patterns_end - &patterns is the patterns size
//&map_end - &map is the map size
//0x0000 & 0x2000 are a vram addresses

下一个代码将显示“电影”:

//char defined in external asm file 
extern char patterns, patterns_end, patterns1, patterns1_end,.......;
extern char palette, palette1,.......;
extern char map, map_end, map1, map1_end,.......;

//now store all addresses inside arrays (is this wrong?)
char *PAT[] = {&patterns,&patterns_end,&patterns1,&patterns1_end,.......};
char *PAL[] = {&palette,&palette_end,&palette1,&palette1_end,.......};
char *MAP[] = {&map,&map_end,&map1,&map1_end,.......};

//now access addresses to use in a function
int main(){
  while(1){   
   bgInitTileSet(0, PAT[v], PAL[v], 0, (PAT[v+1] - PAT[v]), 256*2, BG_256COLORS, 0xC000);
   bgInitMapSet(0, MAP[v], (MAP[v+1] - MAP[v]),SC_32x32, 0x7000);
   v++; 
   WaitForVBlank();
  }
} 

最佳答案

看,patterns 只是一个字符,它不是一个数组。并且patterns_end也只是一个字符。所以你只有两个字符,而不是连续的字符。 您可以执行 for 循环,定义指向patterns 的指针,然后加 1 直到该 aux 指针的地址与patterns_end 地址匹配。 像这样的事情:

char *aux = &patterns;
char *pattern_vector;
int i;

for(i=0; aux!= (&patterns_end) ; i++) {
  pattern_vector[i] = aux;
  aux++;  // Look that here, i am incrementing the address value
}

//Then, here you can use your array

看看是否有效。如果我没有犯错(我做的很快),应该是这样!

关于c - 访问内存地址数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30313394/

相关文章:

c - 每个 winapi 函数的 GetLastError() 错误代码列表

javascript 将数组 1 中的字母替换为数组 2 中的数字并计算这些数字的总和

JQuery:对于每个唯一的数据属性(动态)获取值数组

C 编程 : Initialize pointer to char array inside a function

c - 如何使用指针更改方法内的值

计算C中的一系列字符

php - 排序 multidim 数组 : prioritize if column contains substring, 然后按第二列排序

c - 堆损坏 - 调试断言失败。在 dbgheap.c 行 1322 表达式 _crtIsValidHeapPointer(pUserData)

C - 段错误和 strlen

c - 作为 const 或操作数的正确参数