Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrays of structures

    Hello,

    I'm trying to find a way to refer to a group of declared structs by index. I was planning on declaring an array of pointers, but I'm not sure if this can be implemented, and my current coding is causing an error.

    The relevant code:

    Code:
    struct Node
    {
    	int onpath;
    	double weight;
    };
    Node *nodeArray;
    And it's called by:

    Code:
      for(int i = 0; i < numbers; i++)
      {
    	  getline(myfile, line);
    	  Node l;
          nodeArray[i] = l;
    }
    where numbers is a number read in from the file. The error I'm getting is Unhandled exception at 0x013c2a2b in tempProject.exe: 0xC0000005: Access violation writing location 0x00000000. Is there any way to fix this code so I can refer to each declared node by an index, or is there a better way to keep track of dynamically declared groups of structs? Thanks in advance for any help provided.
    Last edited by gadolinium; 07-12-2012 at 06:27 AM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Where is the code where you are allocating the memory for the pointer? Based on the error it sounds like the nodeArray pointer is null.
    OracleGuy

  • #3
    New Coder
    Join Date
    Jun 2012
    Posts
    15
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Yeah from what it looks like your thing is null, your going to have to malloc inorder to make space in the computer otherwise your going to end up with errors.

    I believe its something along the lines of this...

    Node *nodearray = (Node) malloc(sizeof(Node));

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by michaelh73 View Post
    Yeah from what it looks like your thing is null, your going to have to malloc inorder to make space in the computer otherwise your going to end up with errors.

    I believe its something along the lines of this...

    Node *nodearray = (Node) malloc(sizeof(Node));
    That would only create one node at the pointer. The OP is looking for an array.

    Are you using C or C++? For C you want to use malloc, for C++ you want to use new.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •