question on pointers, structures and graphs

hi

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?

thank you in advance
You'll need to post a bit more code, there's not much to go on there.
here's the whole function call
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void insertVertex (HEAD *pass_head, char x)
{
    int i=0;
    VERTEX *hold_vertex;
    VERTEX *ptr_vertex;

    hold_vertex = (VERTEX *) malloc (sizeof (VERTEX));
    
    hold_vertex->nextV=NULL;
    hold_vertex->name=x;
    hold_vertex->degree=0;
    hold_vertex->processed=0;
    hold_vertex->firstArc=NULL;
    
    pass_head->count++;
    ptr_vertex=(VERTEX *)(pass_head->firstV);
    
    if (pass_head->count==1)
        ptr_vertex=hold_vertex;
    else
    {
        ...
    }
    return;
}

hope it helps
How is the function called? Can you show the code for that and how the first parameter is initialised?

Thanks.
firstly, main function calls buildGraph
1
2
3
	HEAD *graph_head;
	
	graph_head=buildGraph();


inside buildGraph, i called createGraph and insertVertex
1
2
3
4
5
6
7
HEAD *buildGraph (void)
{
	HEAD *temp_head;
	temp_head=createGraph();
	insertVertex(temp_head, 'c');
	return temp_head;
}


and then, its as follow above, where the insertVertex is called
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.
@cire

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?

@cire, @kbw, thanks for helping out
Can you provide the definitions of HEAD and VERTEX please.
kbw wrote:
Can you provide the definitions of HEAD and VERTEX please.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//graph head
typedef struct graphH {
	int count;
	struct graphV *firstV;
} HEAD;

//graph vertex
typedef struct graphV {
	struct graphV *nextV;
	char name;
	int degree;
	int processed;
	struct graphA *firstArc;
} VERTEX;


and just in case you want an overview, i have uploaded into pastebin at http://pastebin.com/JmWnvC3G
Well, you don't actually insert a vertex into the graph, so it's not surprising you can't find the one you 'insert'.

I suspect in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void insertVertex (HEAD *pass_head, char x)
{
    int i=0;
    VERTEX *hold_vertex;
    VERTEX *ptr_vertex;

    hold_vertex = (VERTEX *) malloc (sizeof (VERTEX));
    
    hold_vertex->nextV=NULL;
    hold_vertex->name=x;
    hold_vertex->degree=0;
    hold_vertex->processed=0;
    hold_vertex->firstArc=NULL;
    
    pass_head->count++;
    ptr_vertex=(VERTEX *)(pass_head->firstV);
    
    if (pass_head->count==1)
        ptr_vertex=hold_vertex;
    else
    {
        ...
    }
    return;
}


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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void insertVertex (HEAD *pass_head, char x)
{
         VERTEX* hold_vertex = (VERTEX*)malloc(sizeof(VERTEX)) ;
           
        hold_vertex->nextV=NULL;
        hold_vertex->name=x;
        hold_vertex->degree=0;
        hold_vertex->processed=0;
        hold_vertex->firstArc=NULL;
           
	if ( pass_head->firstV == 0 )
		pass_head->firstV = hold_vertex ;
	else
	{
		VERTEX* ptr_vertex = pass_head->firstV ;

		while ( ptr_vertex->nextV )
			ptr_vertex = ptr_vertex->nextV ;

		ptr_vertex->nextV = hold_vertex ;
	}

	pass_head->count++ ;
}






Last edited on
@cire

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.

P.S., my insertVertex function at the moment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void insertVertex (HEAD *pass_head, char x)
{
	int i=0;
	VERTEX *hold_vertex;
	VERTEX *ptr_vertex;

	hold_vertex = (VERTEX *) malloc (sizeof (VERTEX));
	
	hold_vertex->nextV=NULL;
	hold_vertex->name=x;
	hold_vertex->degree=0;
	hold_vertex->processed=0;
	hold_vertex->firstArc=NULL;
	
	if (pass_head->count==0)
		pass_head->firstV=hold_vertex;
	else
	{
		ptr_vertex=pass_head->firstV ;

		while (ptr_vertex->nextV)
			ptr_vertex=ptr_vertex->nextV ;

		ptr_vertex->nextV=hold_vertex ;
	}

	pass_head->count++;
	return;
}
Last edited on
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++.
thanks for the clarification.
Topic archived. No new replies allowed.