waka-waka-waka
waka-waka-waka

Reputation: 1045

Incompatible pointer type warning C

Thanks for your help in advance. I am trying to create a simple graph in C. I however get

graph.c:66:11: warning: incompatible pointer types initializing
      'edge *' (aka 'struct gegde *') with an expression of type
      'struct gedge *' [-Wincompatible-pointer-types]
    edge *node = fromptr->nextedge;
          ^      ~~~~~~~~~~~~~~~~~
graph.c:69:14: warning: incompatible pointer types assigning to
      'edge *' (aka 'struct gegde *') from 'struct gedge *'
      [-Wincompatible-pointer-types]
        node = node->nextedge;
             ^ ~~~~~~~~~~~~~~
graph.c:75:44: error: incomplete definition of type 'struct gedge'
    printf("val is %c\n", fromptr->nextedge->link->element);
                          ~~~~~~~~~~~~~~~~~^
graph.c:7:12: note: forward declaration of 'struct gedge'
    struct gedge *nextedge;
           ^

Here is my code, I am not sure where I am going wrong. I don't understand why am I getting the incompatible warning and the forward declaration error.

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h> 
#include "graph.h"

typedef struct gegde {
    struct gedge *nextedge;
    struct gvertex *link;
}edge;

typedef struct gvertex {
    ge element;
    struct gvertex *nextvertex;
    struct gedge *nextedge;
    int visited;
}vertex;

void graph_add(ge c, vertex **root, vertex **last)
{
    vertex *temp = *root;
    printf("root points to %p\n", *root);
    while (temp != NULL) {
        // vertex already exists in graph, ignore insert
        if (temp->element == c) {
            return;
        }
        temp = temp->nextvertex;
    }

    vertex *v = malloc(sizeof(*v));
    v->element = c;
    v->nextedge = NULL;
    // first vertex in graph
    if (*root == NULL) {
        *root = v;
        *last = *root;
    } else {
        (*last)->nextvertex = v;
        *last = v;
    }
}

void graph_edge(ge from, ge to, vertex **root, vertex **last)
{
    vertex *temp = *root;
    vertex *fromptr, *toptr;
    int count = 0;
    while (temp != NULL) {
        // ensure both the vertices are present before drawing an edge
        if (temp->element == from) {
            fromptr = temp;
            count++;
        }
        if (temp->element == to) {
            toptr = temp;
            count++;
        }

        temp = temp->nextvertex;
    }
    if (count < 2) {
        printf("either %c or %c vertices do not exist in graph\n", from, to);
        return;
    }

    edge *node = fromptr->nextedge;
    while (node != NULL) {
        printf("found edge %c%c\n", fromptr->element, node->link->element);
        node = node->nextedge;
    }
    edge *newedge = malloc(sizeof(*newedge));
    newedge->nextedge = NULL;
    newedge->link = toptr;
    node = newedge;
    printf("val is %c\n", fromptr->nextedge->link->element);
}

int main(int argc, char **argv)
{
    // keep track of head
    vertex *root = malloc(sizeof(*root));
    root->nextvertex = NULL;
    root = NULL;

    // keep track of last vertext
    vertex *last = malloc(sizeof(*last));
    last->nextvertex = NULL;

    last = root;

    graph_add('a', &root, &last);
    graph_add('b', &root, &last);
    graph_add('c', &root, &last);
    graph_add('d', &root, &last);

    graph_edge('a', 'b', &root, &last);
    graph_edge('a', 'c', &root, &last);
    graph_edge('b', 'c', &root, &last);
    graph_edge('a', 'd', &root, &last);

    return 0;
}

Upvotes: 1

Views: 1419

Answers (1)

ldav1s
ldav1s

Reputation: 16305

Well first there could be a typo:

typedef struct gegde {
    struct gedge *nextedge;
    struct gvertex *link;
}edge;

A struct gegde is not a struct gedge.

This seems to be what the compiler is complaining about.

Upvotes: 3

Related Questions