OneMoreError
OneMoreError

Reputation: 7738

Unable to use pointers in graph

I am new to graphs and I am trying to write very simple programs in graph. I have written two functions, one of which creates an empty graph with as many vertices as inputted by the user. The other one, adds a directed edge between two of the vertices.


The former executes successfully, but the later does not. The program stops running, however the code compiles successfully.


#include <stdio.h>
#include <stdlib.h>
#define MAX 1000

struct node
{
    int data;
    struct node *next;
};

struct node *arr[MAX];

void createEmptyGraph(int n)
{
    // n is the number of vertices
    int i;

    for(i=0;i<n;i++)
    {
       arr[i]=NULL;
    }

    printf("\nAn empty graph with %d vertices has been created",n);
    printf("\nNo edge is connected yet");
}

void addNode(int startVertex,int endVertex)
{
    // For directed edges
    struct node *n;
    n=(struct node *)malloc(sizeof(struct node));
    n->next=arr[startVertex];
    arr[startVertex]->next=n;

    printf("\nAn edge between directed from %d to %d has been   added",startVertex,endVertex);
}

int main(void)
{
    int num;

    printf("Enter the number of vertices in the graph: ");
    scanf("%d",&num);

    createEmptyGraph(num);
    addNode(0,1);

    return 0;
}

I am using adjacency list representation of graphs. Please point out the errror(s).


One more thing, in the createEmptyGraph() method, why can't I do something like this


for(i=0;i<n;i++)
{
    arr[i]->data=d;
    arr[i]->next=NULL;
}

Upvotes: 0

Views: 77

Answers (4)

MvG
MvG

Reputation: 60968

There are several strange things going on in your code.

  • You only have a struct node, no struct edge. As node only has a single next pointer, each node can only have zero or one outgoing edges in this model.
  • addNode looks like it should add edges, but the name says otherwise.
  • As arr represents the nodes of your graph, if addNode really should add an edge, then why does it allocate a new node? And when it should add a node, why doesn't it add that to the array?

As to why your code fails:

arr[startVertex]->next=n;

This will fail, as all entries of arr are initialized to NULL and never changed from that. You can't use -> to access members of a non-existant object.

Upvotes: 0

A.G.
A.G.

Reputation: 1319

CreateEmptyGraph()
for(i=0;i<n;i++)
{
    arr[i]->data=d;    // arr[i] is a pointer that must be initialized first.
    arr[i]->next=NULL; // arr[i] is a pointer that must be initialized first.
}

Since the array arr is an array of pointers you can't call CreateEmptyGraph without initializing arr otherwise you can get access violations.

Upvotes: 0

mathematician1975
mathematician1975

Reputation: 21351

You cannot do this

 for(i=0;i<n;i++) 
 {     
      arr[i]->data=d;     
      arr[i]->next=NULL; 
 }

because your array has not been initialised. The pointers in this array are uninitialised and dereferencing them is not a good idea. Either change the array to be an array of structs instead of struct pointers, or allocate memory to the pointers in your array. Dereferencing uninitialised or NULL pointers is asking for trouble.

Upvotes: 0

Nim
Nim

Reputation: 33645

You declare an array of pointers:

struct node *arr[MAX];

In your createEmptyGraph() method, you need to allocate each structure in the array first - instead you are setting simply the pointer to null..

for(i=0;i<n;i++)
{
    arr[i]=NULL;
}

The following won't work because you've not allocated each entry:

for(i=0;i<n;i++)
{
    arr[i]->data=d;
    arr[i]->next=NULL;
}

Allocate first (malloc()) and then you can set as above...

So, because of the fact that you've not allocated, the following will not work:

n->next=arr[startVertex]; // this is okay, you've set it to NULL
arr[startVertex]->next=n; // ERROR: you are accessing a NULL pointer!

Upvotes: 1

Related Questions