Basic Pointer Help
I have a function that accepts a pointer to a RECT, so an array of any number of RECTs can be passed. Inside the function, the array is copied, element by element, into a member RECT array. Simple so far. My problem is, I don''t know how to declare my lists, or how to pass them to the function, because I don''t know much about pointers. When I try to experiment with various declarations, my code will compile and run, but will crash with an assertion error, so guess-and-check isn''t working. I need to declare an array of RECTS, declare another array which will be resized when my function is called, and pass the first array into my function. The function prototype looks like this:
void setRects( RECT* rectList, int numRects );
Inside setRects(...) I use:
secondRectArray = new RECT[numRects];
//for loop to copy element by element
How do I declare my arrays (RECT* firstRectArray, RECT firstRectArray[], etc.) so that I can copy one array of any size into another? Thanks for the help.
Tolerance is a drug. Sycophancy is a disease.
fisheye|83|:
If whatever initial array length you use will be permanent, something like this can be done:
The problem comes along when you want to try and copy one array to another. If your first array will always be one size or smaller, then the above suggestion will work. If it is constantly growing and shrinking, you''ll probably want to use a linked list, although it will be slower.
Pointers are one of the most difficult ideas to wrap your mind around, but once you do, they''ll be a piece of cake.
John.
If whatever initial array length you use will be permanent, something like this can be done:
const int MAX_NUM = 99; //or whateverRECT *rect_array_ptr = new RECT[MAX_NUM]; // probably should be global
The problem comes along when you want to try and copy one array to another. If your first array will always be one size or smaller, then the above suggestion will work. If it is constantly growing and shrinking, you''ll probably want to use a linked list, although it will be slower.
Pointers are one of the most difficult ideas to wrap your mind around, but once you do, they''ll be a piece of cake.
John.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement