Skip to main content
gamedev.net
🔒 Locked

Array of pointers question

Started by MARS_999 Aug 31, 2003 at 9:15 PM 8 replies 400+ views
Original Post
MARS_999
MARS_999
I feel stupid for asking this beings I use pointer all the time but not to this extent. Here it goes... I have an array of pointers such as this
unsigned char *array[SOME_VALUE];
now I want to access each byte in each element of that pointer array. is this correct??
unsigned char temp = array[0][/*bytes are found here*/];
Thanks
BlueDev
BlueDev
This is one way of how to use a char pointer array:


#include <string.h>

void main() {
unsigned char *array[255];
unsigned char temp[255];

strcpy(temp, "blah"); // write "blah" to temp


array[0] = new char[strlen(temp)+1]; // define array[0]

strcpy(array[0], temp); // write temp to array[0]


// Do whatever you want with array[]


delete [] array[0]; // delete array once your finished

}


Edit: edited source above a bit

Now array[0] equals the word blah.

Hope this makes sense, if not I can help more.

- BlueDev


BlueDev Net

[edited by - BlueDev on August 31, 2003 10:56:47 PM]
[/quote]
MARS_999
MARS_999
I understand that. What I need is accessing each byte of each pointer in the pointer array. Hope that makes sense. I want to have a pointer array that holds however many .tga files imagedata that I need to so I can acces each byte for color. Hope that clears up my problem.
Thanks
drekkar
drekkar
unsigned char tmp = *array[/*element number*/];
MARS_999
MARS_999
quote:
Original post by drekkar
unsigned char tmp = *array[/*element number*/];


But what if I want to access each individual byte in each pointer in the pointer array?


*array[0]//now that I have element 0 I want to access its bytes



Thanks
drekkar
drekkar
ok i think i understand what you are asking now..

unsigned byte specificByte = *(array[0]+#);

where # is the offset of the byte you want to access, 0 is the first (like arrays) 1 is the 2nd etc

// just a quick example of showing how to access them specifically
// this would make the 256 bytes that array[0] is pointing to 255

int i;
for(i=0; i<256; i++){
*(array[0]+i) = 255;
}


[edited by - drekkar on September 1, 2003 1:04:42 AM]
MARS_999
MARS_999
What if I have a multidim array like this how do I access it for address and for variable value?


int **ip = new int*[10];
for(int x = 0; x < 10; x++)
ip[x] = new int[10];

for(x = 0; x < 10; x++)
delete []ip[x];

delete []ip;


Now what I have posted means I have an int array that has int pointers right?

so how do I do this with the posted code above?

int *p = new int(10);
cout << p << "equals address";
cout << *p << "equals value";
//how do I go about this with multidim arrays that are allocated dynamically?



thanks
drekkar
drekkar
its basically the same thing as before but instead of having bytes, it points to a bunch of pointers
*(ip[pointerIndex]+pointerOffset) = value;

pointerIndex is the number from ip[10];
pointerOffset is one of the individual 10 pointers that ip[#] points to

to show the address/value...
cout << "address: " << (ip[0]+0) << endl;
cout << "value: " << *(ip[0]+0) << endl;

the same index/offset thing from above applies to the showing
bagel time ^^


[edited by - drekkar on September 1, 2003 2:48:11 AM]
Cedric
Cedric
Simple.

for(int i=0; i<nb_array; ++i){
unsigned char *one_array = array[i];
for(int j=0; j<size_of_array[i]; ++j){
cout << one_array[j];
}
}

You have to keep the size of each array in size_of_array, but that''s inevitable.

But, as the C++ FAQ Lite will tell you, this is the wrong solution to the problem. The real solution would be to use a std::vector of std::vector.

Cédric
MARS_999
MARS_999
Hey Drekkar is this incorrect or correct. It seems to be working both ways?


//your way

for(int a = 0; a < imageSize; a +=colorMode)
{
colorSwap = *(imageData[index] + a);
*(imageData[index] + a) = *(imageData[index] + (a + 2));
*(imageData[index] + (a + 2)) = colorSwap;
}

//my way

for(int a = 0; a < imageSize; a +=colorMode)
{
colorSwap = imageData[index][a];
imageData[index][a] = imageData[index][a + 2];
imageData[index][a + 2] = colorSwap;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.