CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Pointers

  1. #1
    Join Date
    Nov 2009
    Posts
    11

    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.

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Pointers

    Quote Originally Posted by MSwezey View Post
    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...

  3. #3
    Join Date
    Nov 2009
    Posts
    11

    Re: Pointers

    Quote Originally Posted by VladimirF View Post
    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Pointers

    Where is your loop to add each element of the array?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2009
    Posts
    11

    Re: Pointers

    Quote Originally Posted by Paul McKenzie View Post
    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. : /

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Pointers

    Quote Originally Posted by MSwezey View Post
    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

  7. #7
    Join Date
    Nov 2009
    Posts
    11

    Re: Pointers

    Quote Originally Posted by Paul McKenzie View Post
    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.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointers

    Quote Originally Posted by MSwezey View Post
    Correct me if I am wrong, but isnt
    Code:
    ArrayOfPtrs[0][i]
    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].

  9. #9
    Join Date
    Nov 2009
    Posts
    11

    Re: Pointers

    Quote Originally Posted by Lindley View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured