-
March 24th, 2010, 06:54 PM
#1
Pointers
Hello, writing out some code for a project for college and ran into a rut.
I have an Array of Pointers :
Code:
int * ArrayOfPtrs[3];
ArrayOfPtrs[0] = firstIntPtr;
ArrayOfPtrs[1] = secondIntPtr;
ArrayOfPtrs[2] = sumPtr;
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.
Example:
0 0 1 2 3 4
0 1 2 3 4 5
----------------
0 1 3 5 7 9
I've tried it one way, but comes up with errors and isn't correct.
Thanks for any help, suggestions.
I'll continue to keep working on it.
-
March 24th, 2010, 07:09 PM
#2
Re: Pointers
Originally Posted by MSwezey
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:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
March 24th, 2010, 07:21 PM
#3
Re: Pointers
Originally Posted by VladimirF
It would help if you show WAHT did you try, and what errors did you get.
(ArrayOfPtrs[2]+numDigits) = (ArrayOfPtrs[0]+numDigits) + (ArrayOfPtrs[1]+numDigits)
i tried that, stupidly...error was can't add pointers
so then i tried this
(ArrayOfPtrs[2]+numDigits) = (*ArrayOfPtrs[0]+numDigits) + (*ArrayOfPtrs[1]+numDigits)
and it was also an error.
Thinking about it I know that these two ways can't work, but I can't, at the moment, find a way to write it correctly.
-
March 24th, 2010, 08:08 PM
#4
Re: Pointers
Where is your loop to add each element of the array?
Regards,
Paul McKenzie
-
March 24th, 2010, 08:12 PM
#5
Re: Pointers
Originally Posted by Paul McKenzie
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. : /
-
March 24th, 2010, 08:18 PM
#6
Re: Pointers
Originally Posted by MSwezey
I'm not worried about the loop, I can do that.
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?"
Regards,
Paul McKenzie
-
March 24th, 2010, 09:59 PM
#7
Re: Pointers
Originally Posted by Paul McKenzie
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 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.
-
March 24th, 2010, 11:21 PM
#8
Re: Pointers
Originally Posted by MSwezey
Correct me if I am wrong, but isnt a 2D array?
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].
-
March 24th, 2010, 11:54 PM
#9
Re: Pointers
Originally Posted by Lindley
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].
Ohhh okay! That makes perfect sense!
Thank you for laying it out like that for me!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|