This is clearly not the code you compiled, because it contains a
typo (the word "in" should read "int"). That makes me suspect that
the rest of what you wrote is also not the code you compiled. As
such, the notes I make here are not necessarily the correct notes.
typedef struct Node{
Vertex v;
boolean known;
Distance dist;
Vertex predcssor;
}Node;
then in main() I ve got:
Node *Graph[MAX];
So far so good (modulo the typo). Note that valid values for
indexing the Graph[] array are 0 through MAX-1 inclusive. If
MAX is (say) 200, Graph[0] is OK, and Graph[199] is OK, but
Graph[200] is out of range.
and create structures dynamically:
for(i=1;i<=NUMVERTEX;i++)
Graph= malloc(sizeof (*Graph));
Is NUMVERTEX different from MAX? If so, why? Note that Graph[0]
is not set here, and if NUMVERTEX is >= MAX, Graph[MAX] is used
when it does not exist. In general, in C, loops over arrays mostly
run "for (i = 0; i < N; i++)", not "for (i = 1; i <= N; i++)".
The call:
Graph = malloc(sizeof (*Graph));
has the wrong general format. Calls to malloc should match the
pattern:
var = malloc(N * sizeof *var); /* or when N == 1: */
var = malloc(sizeof *var);
Here "var" is "Graph" (and N is indeed 1), so this should be:
Graph = malloc(sizeof *Graph)
(The parse order for the expression "*Graph" is the same as that
for *(Graph), so there is no need to parenthesize the variable
name Graph in this case.)
for(i=1;i<=NUMVERTEX;i++)
Again, this loop should probably run from 0 to MAX-1 (although your
algorithm probably depends on no node having index number 0). If
you prefer, you can have Graph[0] be deliberately unused, and change
the declaration to:
Node *Graph[NUMVERTEX + 1]; /* note: Graph[0] is never used */
(assuming NUMVERTEX is a constant).
{
Graph->v = i;
Graph->known = NOTINTREE;
Style comment: Graph->known has type "Boolean" (which is presumably
just "int", unless you are using C99, in which case why not stick with
C99's "bool" in the first place?). "Proper" Boolean values are just
TRUE and FALSE; "NOTINTREE" is neither TRUE nor FALSE.
if(atoi(argv[2]) == i)
Graph->dist = 0; //this is the source so set distance to zero
else
Graph->dist = INFINITY; //distance from the source
set to INFINITE
Usenet-specific: Notice how the "//" comment text has changed, so
that the comment terminates at the word "source". The rest of the
comment has now become an (invalid) line of C code, which will draw
a diagnostic -- probably "syntax error" -- from the compiler. "//"
comments do not work well in code that may be reformatted by Usenet
News systems. "/*" comments do not suffer from this particular
problem.
Style-and-substance: atoi(argv[2]) has to "do work" to calculate
the "int" value of the series of digit characters in argv[2][0],
argv[2][1], argv[2][2], ..., up until argv[2] is '\0'. Unless
the compiler can prove to itself that atoi(argv[2]) returns the
same result on every trip through the loop, the compiler will have
to re-compute that value every time. It would make a lot more
sense to instruct the system to compute it once, and also give it
a name, e.g.:
int source_vertex;
if (argc < 3) {
... /* handle error, not enough arguments */
}
source_vertex = atoi(argv[2]);
/* now you can also check that source_vertex is a sensible number */
...
for (...) {
Graph->v = i;
Graph->known = FALSE; /* or whatever */
/* the source vertex is at distance 0; others are at INFINITY
until later calculation says otherwise */
Graph->dist = i == source_vertex ? 0 : INFINITY;
Graph->predcssor = NULL; //predecessor in the path set to NULL
}
According to the "struct Node" definition, the "predcssor" field
has type Vertex, which is an alias for "int" (assuming the typo
correction I made was the right one). NULL is not a suitable
value for "int"s, as it may -- at the C compiler's discretion --
be defined as ((void *)0).
If you are using the "Graph[0] is never used so that I can use
index 0 to mean no index" trick (as I suspect you are), and if
you really do mean to have the "predcssor" field contain the
index number of the predecessor (as opposed to, say, a value of
type "pointer to struct Node" pointing to the same place Graph[x]
would point to for predecessor node #x), just set it to 0.
If you *do* want predcssor to have type "struct Node *", give it
that type.
I run gdb to debug my code. After I allocate memory and get into the second
for loop, for the second time, I find out that there is no member w of the
first struct, ie I write "print Graph[1]->w" and I get there is no member
named w ...
There is indeed no member named "w". There are four members,
named "v", "known", "dist", and "predcssor".