Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ Problem

  • 25-03-2006 12:29PM
    #1
    Registered Users, Registered Users 2 Posts: 871 ✭✭✭


    Hey, I'm just wondering how do you pass a value from an integer to an address in an array pointer, (also int)

    Any help would be greatly appreciated. Thanks
    Ger...


Comments

  • Closed Accounts Posts: 17,163 ✭✭✭✭Boston


    X= your integer value
    Y = your address

    *Y = X (the think Y points to is assigned the value X)

    That should be it from what I remember.

    Edit, I didn't read your post fully.

    So you have something like this

    typedef int* IntPtr

    IntPtr a;
    a = new IntPtr[size_of_block]

    //Now you have an array of pointers.
    //To assign an individual pointer, do like you would normally.

    *a[2] = x (where X is a value)


  • Registered Users, Registered Users 2 Posts: 871 ✭✭✭gerTheGreat


    that's it thanks a mill
    ger...


  • Closed Accounts Posts: 60 ✭✭goldilocked


    This looks dodgey to me...

    You've created an array of size integer pointers.

    You're then storing the value x in the address contained in the second array entry. But you have not allocated this storage.


  • Closed Accounts Posts: 17,163 ✭✭✭✭Boston


    True, but the question was how to assign pointers in an array. The bit of code demonstates that, it doesn't demonstate how to set up a pointer array. Just include a for loop that assigns storage to each of your pointers in the array

    for(int i = 0; i<size;i++){
    a = new int;
    }


  • Closed Accounts Posts: 60 ✭✭goldilocked


    Well if the OP's learning how to program, they may aswell also learn our pedantic ways!


  • Advertisement
Advertisement