Graph Algorithm in C

Bluga

Banned
Nov 28, 2000
4,315
0
0
This is the code for inserting Vertex and connect edges for GRAPH (from a textbook). I was wondering why do we need two task pointers (task1, task2) for creating Vertex and deleting edges. For example, the following code creates two Vertex: A, B and establishes an edge between A and B. Thanks for any help.


/**********************************************/
int main(int argc, char **argv) {

Graph graph;
DfsVertex *task, task1, *task2;
List list;
ListElmt *element;
char data1[STRSIZ];
int i;

/*-----------------Initialize the graph-------------------------*/

graph_init(&graph, match_task, destroy_task);


/*----------- Insert Vertex A and B-----------------------*/

if ((task = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;

if ((task->data = malloc(STRSIZ)) == NULL)
return 1;

strcpy((char *)task->data, "A");
fprintf(stdout, "Inserting vertex %s\n", (char *)task->data);

if (graph_ins_vertex(&graph, task) != 0)
return 1;

if ((task = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;

if ((task->data = malloc(STRSIZ)) == NULL)
return 1;

strcpy((char *)task->data, "B");
fprintf(stdout, "Inserting vertex %s\n", (char *)task->data);

if (graph_ins_vertex(&graph, task) != 0)
return 1;

/*-------------Inserts the edge between A and B ------------------*/

task1.data = data1; <===what is the purpose of this line?

if ((task2 = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;

if ((task2->data = malloc(STRSIZ)) == NULL)
return 1;

strcpy((char *)task1.data, "A");
strcpy((char *)task2->data, "C");
fprintf(stdout, "Inserting edge %s to %s\n", (char *)task1.data, (char *)
task2->data);

if (graph_ins_edge(&graph, &task1, task2) != 0)
return 1;
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
Yikes, that is some ugly code.

DfsVertex apparently has a member "data" which is a pointer to a char...

They did not want to allocate task1.data, so they statically defined another char data1[STRSIZ]

the line: task1.data = data1
is nothing but a pointer "transfer" - since one variable is most likely defined as pointer, and the variable name to an array is really handled as a pointer as well.

They could have done this by doing a:

if ((task1.data = malloc(STRSIZ)) == NULL)
return 1;


instead.
 

Bluga

Banned
Nov 28, 2000
4,315
0
0
Thanks a lot!

I've another question, in the same program instead od using :

strcpy((char *)task->data, "A");

I use:


i=1;
strcpy((char *)task->data, i );


why do i get segmentation fault?? How can i convert integer to character?
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
Well, there is also a trick you can use.

First of all, do you want it to contain a binary one, or the character one (those are two different things!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)

If you want a binary one, it's easy.
If you want a character one, you have to add 0x030 (hex 30 = the character "0") or 48

and then you can use a trick. A string is anything NULL terminated. Any integer (they ARE at least 16 bit) are NULL terminated if smaller than 256.
But you again need a pointer (a string variable is always a pointer)...

you could do
i=1;
strcpy((char *)task->data, &i );

or

i=1;
i+=0x30; //alternatively i+=48;
strcpy((char *)task->data, &i);

It's kinda ugly, but it will work.

Edit: fixed the fact that ; ) will be made a ;) when written together.