And each of these Pointers point to an int Array that is Dynamically Allocated.
Code:
int * firstIntPtr;
firstIntPtr = new int[sizeOfIntArray];
int * secondIntPtr;
secondIntPtr = new int[sizeOfIntArray];
int * sumPtr;
sumPtr = new int[sizeOfIntArray];
I've passed the Array of Pointers to a function outside of main that is suppose to add the firstIntPtr and secondIntPtr together cell by cell using the paper and pencil algorithm and then putting that in the sumPtr Array.
The problem i ran into is deals with the Array of Pointers, each Pointer in the array points to the address of the first cell of the first, second, and sum arrays. I am having difficultly getting the pointers to point to the last cell and working its way back to the beginning.
I've tried it one way, but comes up with errors and isn't correct.
It would help if you show WAHT did you try, and what errors did you get.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
Where is your loop to add each element of the array?
Regards,
Paul McKenzie
I'm not worried about the loop, I can do that.
I'm just worried about how to get these pointers to point to something else then just the first cell of each array.
Talked to a friend of mine, he just passed three different pointers as parameters instead of an array of pointers. I might do that, but will get some points deducted off. : /
The code you posted suggests you are forgetting about the loop altogether, causing the issues you're seeing now.
I'm just worried about how to get these pointers to point to something else then just the first cell of each array.
Arrays are contiguous. Once you point to the first cell, you get to cell 2, cell 3, cell 4, by looping with a simple index. You don't have to "repoint" the pointers.
Code:
for (int i = 0; i < 5; ++i)
{
std::cout << ArrayOfPtrs[0][i] << "\n";
}
What do you get as output when that loop is executed? You get the numbers in the first array. So that is why I asked you, "where is your loop?"
The code you posted suggests you are forgetting about the loop altogether, causing the issues you're seeing now.
Arrays are contiguous. Once you point to the first cell, you get to cell 2, cell 3, cell 4, by looping with a simple index. You don't have to "repoint" the pointers.
Code:
for (int i = 0; i < 5; ++i)
{
std::cout << ArrayOfPtrs[0][i] << "\n";
}
What do you get as output when that loop is executed? You get the numbers in the first array. So that is why I asked you, "where is your loop?"
Regards,
Paul McKenzie
Correct me if I am wrong, but isnt
Code:
ArrayOfPtrs[0][i]
a 2D array?
So the pointers get me to the address of the array[0], but how can i jump to the back of the array when all i have to access that array is the pointer itself?
I'm sorry, I'm not trying to be a pain. Just trying to understand this stuff for myself and learn from it.
Last edited by MSwezey; March 24th, 2010 at 10:03 PM.
In a sense, yes. But in a sense, a 2D array is precisely what you have, so that fits. The first dimension has size 3, and the second dimension has size sizeOfIntArray.
It isn't the same memory layout as you'd get putting a 2D array on the stack, but it's conceptually similar and the same syntax is used to access it.
Put simply: If A has type T*, then the expressions *(A+i) and A[i] are equivalent. Since ArrayOfPtrs[0] has type int*, therefore you'd access the elements of the array as ArrayOfPtrs[0][i].
In a sense, yes. But in a sense, a 2D array is precisely what you have, so that fits. The first dimension has size 3, and the second dimension has size sizeOfIntArray.
It isn't the same memory layout as you'd get putting a 2D array on the stack, but it's conceptually similar and the same syntax is used to access it.
Put simply: If A has type T*, then the expressions *(A+i) and A[i] are equivalent. Since ArrayOfPtrs[0] has type int*, therefore you'd access the elements of the array as ArrayOfPtrs[0][i].
Bookmarks