Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Cannot copy address of array with pointer

0 votes
asked May 7, 2019 by anonymous
I want to copy the address of array into temp

but temp is still 0x0 when parse the line" temp = cellRowHeads[i];"

Cell** createBoard(int height, int width)

{

    Cell** cellRowHeads = new Cell*[height];

    for(int i=0; i<height; i++){

        Cell* temp = nullptr;

        temp = cellRowHeads[i];

        for(int j=1; j<width; j++){

            Cell* cell = new Cell;

            temp->next = cell;

            temp = cell;

        }

    }return cellRowHeads;

}

2 Answers

0 votes
answered May 8, 2019 by anonymous
0x0 is an address that is reserved for the system so u can't write to it
0 votes
answered May 14, 2019 by organicoman

try always to use C++ facilities, like type aliasing.

using ptrCell = Cell*;
/*or: typedef Cell* ptrCell; */

now your code will look like the following, and it is much clearer what happening:

ptrCell createBoard(int width, in height)

{

    ptrCell* cellRowHeads = new ptrCell [Height]; // ptrCell* is an array of Cell* all initialized to nullptr by default

    for( int i=0; i< height; i++)

    {

        ptrCell temp = nullptr;

        temp = cellRowHeads[i]; // copy assignment of an element of the array which equals to nullptr, read above

        for (int j=0; j<width; j++)

        {

           ptrCell cell = new Cell;

           temp->next = cell; // BOOOM, arrow operator on a nullptr, Segmentation fault.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...