0
  struct Node {
      int data;        // The data being stored at the node
      Node *next;     // Pointer to the next node
      };

int main()
{
         Node **nodeArray = new (Node*)[5];
}

First question:

Is the statement in main a valid way to create an array of 5 Node * 's ?

What is the difference between the statement in main and Node **nodeArray = new Node*[5];? Main currently gives me an error: array bound forbidden after parenthesized type-id|

Second question:

How would I go through the array and do a new for each one of them? I've worked with arrays and I've worked with linked lists, but putting them together seems trickier than I'd thought.

2
  • You shouldn't have the parentheses around Node* after new.
    – ooga
    Oct 15, 2014 at 3:05
  • Better to use std::vector<Node*> (or std::vector<std::unique_ptr<Node>>).
    – Jarod42
    Oct 15, 2014 at 7:18

2 Answers 2

3

If you know that you need maximum of 5 items, you should use static allocation because it's faster and you don't have to worry about deallocating the array.

Node* array[SOME_CONST];
for (int i=0; i < SOME_CONST; i++)
{   
   array[i] = new Node()
   cout<<array[i];
}

For dynamically allocated arrays is pretty much the same thing, you just have to realize the pointer new returns points to the first item in the array.

Node** array = new Node*[some_num];
for (int i=0; i < some_num; i++)
{
   array[i] = new Node();
}

Don't forget to deallocate properly:

for (int i=0; i < some_num; i++)
{
   delete array[i];
}
delete[] array;
5
  • I get an error when I try your new statement. Please look at my addition to the first question in the original post. Oct 15, 2014 at 3:12
  • Yeah, you are right. The () should not be there. I'll update my example.
    – ventsyv
    Oct 15, 2014 at 3:15
  • Your dynamic array version does not compile, error on the new statement in the for loop: no match for 'operator=' in '(( array) + ((sizetype)(((unsigned int)i) * 8u))) = (Node*)operator new(8u)'| Oct 15, 2014 at 3:32
  • Sorry, it's getting late and I'm getting sloppy. Try it now.
    – ventsyv
    Oct 15, 2014 at 3:47
  • Worked like a charm,thanks. I really need to brush up on pointers. Oct 15, 2014 at 3:51
1
  1. Yes.

  2. Just loop over your 5 Node*s and assign to each one of them a new Node. Or if you're trying to make a linked list, maybe assign each one of them to the next one of the 5.

3
  • I appreciate your answer. What does my current statement in main doing, though? I thought I just made 5 node pointers. Oct 15, 2014 at 3:07
  • Yes, it makes a pointer to an array of 5 pointers to Node...but those pointers do not point to anything yet. Oct 15, 2014 at 3:09
  • There is a question about that here: stackoverflow.com/questions/16546319/…
    – ventsyv
    Oct 15, 2014 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.