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

Thread: Pointers

Hybrid View

  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

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