0

I'm attempting to do a DFS search on a certain graph, here I have attempted to write down the following code (I'm aware that there are many errors/warnings and mistakes I have made but this is only a start and I need some help).

int visited[];
static int last=-1;
int record_of_visit[];


void print_dfs(Graph* graph, int source_id) 
{
    //printf("not yet implemented: put code for part 1 here\n");
    visited[source_id]=1;                
    record_of_visit[++last]=source_id; 
    int i;
    for (i=0;i<20;i++) 
    {  
        if (graph[source_id][i]==1) 
        {
            if(visited[i] == 0)  
                print_dfs(graph,i); 
        }
    }     
}   

The graph.c file implementing the adjacency list can be found here.

The graph.h file can be found here.

Here I am trying to access the graph which is an already built adjacency list, but the compiler would give me error when I run this program indicating that on if (graph[source_id][i]==1)

subscripted value is neither array nor pointer

What am I doing wrong here? Also any tips or suggestions on what to improve in my still raw DFS implementation would be appreciated!

2Xchampion
  • 666
  • 2
  • 10
  • 24
  • 1
    your paste on pastebin is private. – yar Apr 02 '17 at 03:12
  • 1
    `Graph* graph` in your function declarations suggests `graph` only a pointer to `Graph`, not a pointer to pointer to `Graph`. Hence, derefencing it twice with `[source_id][i]` will not work. –  Apr 02 '17 at 03:12
  • You could at least include the definition of `Graph` somewhere in your question; external links are not a good idea (@yar's comment is just one reason why). –  Apr 02 '17 at 03:13
  • @Evert Sorry for the confusion I have made it public for access.. Just thought that posting that long chunk of codes would make people feel irritated...hence chose to add a link... – 2Xchampion Apr 02 '17 at 03:15
  • please add graph.h, too, I suppose there is the declaration of Graph – yar Apr 02 '17 at 03:21
  • @yar Done. Thank you for the heads up! – 2Xchampion Apr 02 '17 at 03:24
  • @jenesaisquoi Yeah I was struggling on finding an appropriate way to get the length of lists. For now I'm sticking with a finite number as the assumption. But thank you for the suggestion! – 2Xchampion Apr 02 '17 at 03:38
  • @jenesaisquoi Np! – 2Xchampion Apr 02 '17 at 03:39
  • What do you want to access with `graph[source_id][i]`? – yar Apr 02 '17 at 03:50
  • @yar I reckon it is wrong to do so, but I am trying to use the adjacency list (the one that graph points to) and loop through it, basically I do not know how to access it and loop it... – 2Xchampion Apr 02 '17 at 03:51

1 Answers1

0

You can access elements of a struct by a "dot", i.e. you can acces n from the first graph in your array by graph[0].n

yar
  • 1,855
  • 13
  • 26
  • Thing is the error I have indicates that the graph itself is not an array or something (hence I cannot de-reference it ), but it points to some lists/vertices, is there something like graph->vertices[a]->first_edge? – 2Xchampion Apr 02 '17 at 04:03
  • `struct graph ` is a structure, that contains three elements: `n `, `maxn ` and `vertices`. You can acces the pointer of pointers `vertices` with `graph[i].vertices`. Then you can access an element e.g. by `*(graph[i].vertices[0])`. Nevertheless, `graph` does not contain pointers to `graph`, so the recursion with `Graph*` will not work this way. – yar Apr 02 '17 at 04:31