Advertisement

Using pointers to pass and manipulate an array?

Started by October 03, 2002 10:06 AM
4 comments, last by Timbo_M45 22 years, 1 month ago
I'm using DIB image manipulation for some of the graphics manipulation routines in my game's engine, and I need to pass an array of bytes to a function. This array's values are then modified, and then the SetDIBitsToDevice API call is used to send the bitmap back to the target Device Context. The problem I ran into was simple, I'm new to C++ and have been using VB for a VERY long time, so pointers are new to me. How do I use a pointer to pass the array to the function, and then how do i manpulate values in the array? here's an example of what I'm trying to do, note that this is not the actual code but it shows how i tried to use the array.

void myroutine (HDC hDestDC, BITMAPINFO *biSrcInfo, char *bBytes[]) {

//bBytes is a pointer to the array of bytes.
// THE BELOW CODE WOULD NORMALLY BE IN A FOR LOOP...
     
//in this example we basically set the pixel color of the bottom-left most pixel of the bitmap.
     bBytes = BLUE_VALUE   //set array element 0
     ++bBytes = GREEN_VALUE //set array element 1
     ++bBytes = RED_VALUE  //set array element 2

}
   
Ok, well thats how i've been trying to change values in my byte-array, but i aint even sure if thats the problem, my program causes a fatal error when i call this function, and this may be where the real problem is - when i call SetDIBitsToDevice I dont know if I'm passing the pointer to the array correctly. In one example on CodeGuru.com I saw them call the SetDIBitsToDevice function as follows:

// Let's draw the grayscale image
		LPVOID lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
		::SetDIBitsToDevice(pDC->m_hDC, xDest, yDest, nWidth, nHeight, 0, 0, 0,
				nHeight, lpDIBBits, (LPBITMAPINFO)hDIB, DIB_RGB_COLORS);
   
Someone help, maybe provide a solid example of howto do this. Do i need to somehow convert my array of bytes to LPVOID? and if so, HOW? NOTE that I originally wrote this routine in VB but am now converting it to C++, it worked fine in VB, I am like 99% sure my problem has something to either do with my handleing of the pointers to the arrays, and manipulating the array element values, OR me trying to pass *bBytes to the SetDIBitsToDevice API Call... [edited by - Timbo_M45 on October 3, 2002 11:10:48 AM]
Any pointer type can implicitely be cast to void* (well, maybe const void* or mutable void* depending on the qualifiers), you don''t have anything to do.

There is an error here :
bBytes = BLUE_VALUE   //set POINTER to BLUE_VALUE++bBytes = GREEN_VALUE //increment POINTER, then set it to GREEN_VALUE++bBytes = RED_VALUE  //increment POINTER, then set it to RED_VALUE 


You''re only modifying the pointer. At no point are you ever touching the pointed values.

The (trivial) fix is left as an exercise to the reader.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Try changing "char *bBytes[]" to "char bBytes[]" or "char *bBytes". "char *bBytes[]" indicates an array of pointers, which in C really means a pointer to a pointer, since arrays are treated as pointers.

To reference bBytes in your function, just use an indexing variable, e.g., bBytes = ... Or you can use the pointer syntax, e.g., *bBytes = ... , but you''ll have to increment bBytes as you go in that case instead of incrementing "i".

In calling the function you would just have your array variable name in the parameter. The compiler knows its a pointer (since it''s an array), so you will actually be passing in a pointer to the array.
damn, forgot about the italics converter:

"e.g., bBytes = ..." should really read "e.g., bBytes[index] = ..."
trivial fix huh? well that dont help me too much. If someone could expand on what he was saying.. i thought that the ++bBytes would increment the pointer to the next array element, and the = would in-turn set the value of that array element. ++ before the variable bBytes increases bBytes before performing the ''='' operation, right? someone shed some light on this for me PLEASE!?!
Hi Timbo,

Pointers can look really confusing at first, but soon you''ll forget that they were ever a problem. Just hang in there.

If you want to treat an array like a pointer, there are a few ways to do it. Here are the two easiest.

one way:
char myArray[5];
char *myPointer;

myPointer = myArray;

This works because myArray is really a pointer, just a special type.

the other way:
char myArray[5];
char *myPointer;

myPointer = &myArray[0];

In this case you are saying myPointer is equal to the memory address of the first element in myArray.

Remember that pointers are just memory address. The pointer itself is a 32-bit number, thus 4 bytes long, and stores a memory address. The TYPE of the pointer is there to make your life easier. ie:

char *myChar;
long *myLong;

myChar++;
myLong++;

the memory address stored by myChar has been increased by 1 byte (the length of a char), and the address stored by myLong has been increased by 4 (the size of a long).

So, now lets fix your problem.

In this line:

void myroutine (HDC hDestDC, BITMAPINFO *biSrcInfo, char *bBytes[])

specifcally the char *bBytes[].

Lets look at an array:

char myArray[5];
In this case myArray is 5 chars.

char *myArray[5];
In this case myArray is 5 pointers (5 memory address).

How does this affect you? In the first case you have 5 characters. In the second, you have five memory address.

What you want to do, to make you code work is change the char *bBytes[] to char *bBytes. (loose the []).

Then when you call this function, just pass the ADDRESS of your array.

Here''s an example.

main
{
char myArray[5];
char result;

result = myFunction ( myArray);
}

char myFunction ( char *data)
{
char result;

*data = ''a''; // Set element 0 to ''a''
data++; // Next element
*data = ''b''; // Set element 1 to ''b''
data++; // Next element;

result = *data;
return result;
}

I hope this helps. Most people have a hard time with pointers starting out. If you stick with it long enough though, you''ll wonder how you ever got along without them!

Will

------------------http://www.nentari.com

This topic is closed to new replies.

Advertisement