简体   繁体   English

如何访问大小不同的字节数组?

[英]how can i access to a byte array of array of different size?

i facing a problem when i try to access to an array of array with a pointer, i have several arrays declared as following : 当我尝试使用指针访问数组数组时遇到问题,我声明了以下几个数组:

BYTE a[5] = {0xcc,0xaa,0xbb,0xcc,0xff};
BYTE b[3] = {0xaa,0xbb,0xff};

thos BYTE arrays represents image that i want to load in memory from a dll, i can access them separately with no difficulty, but i want to access them from a loop with a pointer ... thos BYTE数组代表我要从dll加载到内存中的图像,我可以毫不费力地单独访问它们,但是我想从带指针的循环访问它们...

i tried to put them into an array of array like this : 我试图将它们放入这样的数组数组中:

BYTE* c[2] = {a,b};

but when i want to loop through this pointer c[i] doesent load image at i index into memory, how could i fix it? 但是,当我想遍历该指针c [i]并在索引中将图像加载到内存中时,我该如何解决?

im trying to draw images with this method within a loop to avoid repating lines ( consider c is images[i]) 我试图用这种方法在一个循环中绘制图像以避免重复线条(考虑c是images [i])

void MenuIcon::drawIcons(LPDIRECT3DDEVICE9 npDevice)
{
    isMenuVisible = (Pos.x < 100) && (Pos.x > 0)?  true : false;

    if(isMenuVisible)
    for (int i = 0; i < 2; i++)
    {
        if (icon[i].img == NULL)D3DXCreateTextureFromFileInMemory(npDevice, &images[i], sizeof(images[i]), &icon[i].img);
        DrawTexture(icon[i].coord.x, icon[i].coord.y, icon[i].img);
    }

}

i try to use my method like this after reading your answer : 阅读您的答案后,我尝试使用这种方法:

 void MenuIcon::drawIcons(LPDIRECT3DDEVICE9 npDevice)
{
    isMenuVisible = (Pos.x < 100) && (Pos.x > 0)?  true : false;

    if(isMenuVisible)
    for (size_t i = 0; i < images.size(); i++)
    {

        std::vector<BYTE>& curArray = images[i];
        if (icon[i].img == NULL)D3DXCreateTextureFromFileInMemory(npDevice, &curArray, curArray.size(), &icon[i].img);
        DrawTexture(icon[i].coord.x, icon[i].coord.y, icon[i].img);
    }

}

but it still not drawing nothing .... maybe &curArray is not called proprely?? 但是它仍然没有画出任何东西....也许&curArray没有被适当地调用? NB -> i logged both image and size of arrayofarray and it return me correct values.... 注意->我记录了图像和arrayofarray的大小,它返回了正确的值...。

You can use std::vector : 您可以使用std::vector

#include <vector>
#include <iostream>
#include <iomanip>

typedef int BYTE;
typedef std::vector<BYTE> ByteArray;
typedef std::vector<ByteArray> ArrayOfByteArray;

using namespace std;

int main()
{
   // fill our array
   ArrayOfByteArray c = {{0xcc,0xaa,0xbb,0xcc,0xff}, 
                         {0xaa,0xbb,0xff}
                         // add more as desired...
                         };

   // loop on each entry
   for (size_t i = 0; i < c.size(); i++)
   {
      // get the byte array for entry i
      std::vector<BYTE>& curArray = c[i];

      // output the values found for this array
      for (size_t j = 0; j < curArray.size(); ++j)
         cout << std::hex << curArray[j] << " ";

      cout << "\n";
   }
}

Live Example 现场例子

The std::vector knows the sizes of each of the entries, unlike an array. 与数组不同, std::vector知道每个条目的大小。

Take the code here and add to it to suit what you're attempting to do with the images. 在此处获取代码并添加到其中,以适合您要对图像执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM