0

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 ...

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?

im trying to draw images with this method within a loop to avoid repating lines ( consider c is 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?? NB -> i logged both image and size of arrayofarray and it return me correct values....

10
  • 1
    You can't. You need to allocate memory for both and copy them there. Commented Jan 5, 2016 at 20:06
  • 1
    You need to know the actual sizes of the array you have pointers to. One way in C++ is to not use C-style arrays, but std::array (or optionally std::vector) instead. Commented Jan 5, 2016 at 20:06
  • its very annoying :(, if i have like 100 images how could i do to be more efficient?, someone could provide me a snippet or hint to doit like it should be done? Commented Jan 5, 2016 at 20:09
  • 1
    again, what are you trying to do? The declaration you have made is actually doing something. It is declaring an array of pointers. Which is good if you don't mind to iterate them in a two-dimensional fashion. but you need to know sizes. Commented Jan 5, 2016 at 20:14
  • i edited my question with more explanation ;) Commented Jan 5, 2016 at 20:17

1 Answer 1

3

You can use 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.

Take the code here and add to it to suit what you're attempting to do with the images.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.