i'm working on my assignment, and it requires me to use graphs. here's a snippet of the code
1 2 3 4
ptr_vertex=(VERTEX *)(pass_head->firstV);
if (pass_head->count==1)
ptr_vertex=hold_vertex;
the code above does not work, and produces an error that states Unhandled exception at 0x012f1c6c in test.exe: 0xC0000005: Access violation reading location 0x00000004.
to note, vertex is a structure, pass_head is the head structure for the graph, firstV is the first vertex.
what i intended to do was, use hold_vertex to hold all the information. ptr_vertex will be used to move to the next vertex, and hold_vertex will then transfer all the details into ptr_vertex. can anyone point out where i went wrong?
I'm curious why passhead->firstV requires a cast when assigning the value to ptr_vertex.
Also, you say you intended to use ptr_vertex to iterate the existing vertices, but clearly assign it the address that hold_vertex contains in the case of no existing vertices. If you later treat this as the address of an already inserted vertex, that will be a problem.
I'm curious why passhead->firstV requires a cast when assigning the value to ptr_vertex.
i'm not sure myself. i tried it without a cast, and debugging mode showed that the pass_head did not store any hold_vertex values, when it should.
you say you intended to use ptr_vertex to iterate the existing vertices, but clearly assign it the address that hold_vertex contains in the case of no existing vertices. If you later treat this as the address of an already inserted vertex, that will be a problem.
i intend to have ptr_vertex to point to the location where there is no vertex.
in the first insert, it should point to pass_head->firstV, as to fill in the first vertex.
in the second insert, it should point to pass_head->firstV->nextV.
is my logic wrong? :/ if so, how should it be done then?
you think line 16 somehow makes it so that further assignments to ptr_vertex somehow affect pass_head->firstV, which is not the case. When you assign to ptr_vertex again, you simply change the address contained in ptr_vertex from 0 (which is what was in head->firstV) to wherever hold_vertex resides in memory.
thanks for pointing out line 16. i guess it didn't work the way i wanted it to be.
anyways, i have tried your code above, and it worked. i tried inserting three vertex information, and all of them are accessible from the main function.
however, i wish to ask, on how you declared ptr_vertex. it was declared in the middle of the code. i'm doing this assignment in visual C, so i guess that's not possible?
on a side note, thank you again for helping me out.
however, i wish to ask, on how you declared ptr_vertex. it was declared in the middle of the code. i'm doing this assignment in visual C, so i guess that's not possible?
It is perfectly legal in C to declare variables at the beginning of a block. In a C99 compliant compiler I believe you can declare them anywhere in a function just as you would in C++.