Thread: Array of pointers

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    36

    Array of pointers

    Hi, im using visual c++ and I am getting run time errors whenever i try this problem.

    What i am trying to do it create an array of linked lists. So for example in array[x][y] I would like to store the node at the head of the list.

    Could someone advise me how to declare this array and how to use it / pass it to functions?

    Thank you


    - this is a piece of code im using now. This function takes the pointerarry, an x-y coordination and number to put in the linked list.
    Code:
    void createlist(int x, int y, int value, nodeptr *pointerarray[9][9]){
    
    	nodeptr hdList;
    	hdList = new node;
    	hdList->number = value;
    	hdList->next = NULL;
    	pointerarray[x][y] = &hdList;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks like you are trying to store the address of the local pointer, rather than a pointer to the allocated node.

    Perhaps this works better:

    Code:
    void createlist(int x, int y, int value, nodeptr pointerarray[9][9]){
    
    	nodeptr hdList;
    	hdList = new node;
    	hdList->number = value;
    	hdList->next = NULL;
    	pointerarray[x][y] = hdList;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    great man! thank you. It seems to be doing what I want now

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Suggestion: Avoid typedefing type* things because it makes the code less readable, especially when used wrong. I see not harm in typing type* instead of typeptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM