...

View Full Version : Arrays of structures



gadolinium
07-12-2012, 05:20 AM
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:



struct Node
{
int onpath;
double weight;
};
Node *nodeArray;


And it's called by:



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.

oracleguy
07-12-2012, 05:48 PM
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.

michaelh73
07-12-2012, 08:08 PM
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));

oracleguy
07-13-2012, 12:07 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum