-3

I was trying to convert this code from C++ to C. It should find the Eulers cycle of the graph, but that is not really important here. My problem is that I don't know why I receive this compiling errors.

Code in C++:

#include <iostream>
#include <iomanip>

using namespace std;

// Typy danych

struct dlistEl
{
      dlistEl *next,*prev;
  int v;
};

// Zmienne globalne

int m,n;                        // Liczba krawędzi i wierzchołków
char **graf;                    // Dynamiczna macierz sąsiedztwa
bool * visited;                 // Tablica odwiedzin


void addC(int x, dlistEl *p)
{
  dlistEl * r;

  r = new dlistEl;
  r->v = x;
  r->next = p->next;
  if(r->next) r->next->prev = r;
  r->prev = p;
  p->next = r;
}

// Procedura usuwa z listy element wskazywany przez p
//---------------------------------------------------
void remC(dlistEl *p)
{
  if(p->next) p->next->prev = p->prev;
  if(p->prev) p->prev->next = p->next;
  delete p;
}

// Rekurencyjna funkcja dodająca do listy nowy cykl
// v - wierzchołek startowy i końcowy cyklu
// w - wierzchołek bieżący
// p - referencja do wskazania punktu wstawiania na liście
//--------------------------------------------------------
bool DFSaddCycle(int v, int w, dlistEl * & p)
{
  int u;

  visited[w] = true;            // Oznaczamy v jako odwiedzony
  addC(w,p);                    // Dodajemy w do cyklu
  p = p->next;                  // p wskazuje dodany element
  for(u = 0; u < n; u++)        // Przeglądamy sąsiadów w
    if(graf[w][u])
    {
      if(u == v)                // Cykl znaleziony?
      {
        addC(v,p);              // Zamykamy cykl na liście C
        do
        {
          graf[p->v][p->next->v] = 0; // Usuwamy krawędzie cyklu
          if(p->v == v) return true;
          p = p->prev;            } while(true);
      }
  if(!visited[u] && DFSaddCycle(v,u,p)) return true;
    }
  p = p->prev;                 // Z listy usuwamy w
  remC(p->next);
  return false;
}

// **********************
// *** PROGRAM GŁÓWNY ***
// **********************

int main()
{
  int i,j,v1,v2;
  dlistEl *C,*p;

  cin >> n >> m;                // Czytamy liczbę wierzchołków i krawędzi

  // Tworzymy tablice dynamiczne

  graf    = new char * [n];
  visited = new bool [n];
  for(i = 0; i < n; i++)
  {
    graf[i] = new char [n];
    for(j = 0; j < n; j++) graf[i][j] = 0;
  }

  // Odczytujemy definicje krawędzi grafu

  for(i = 0; i < m; i++)
  {
    cin >> v1 >> v2;
    graf[v1][v2] = 1;
  }

  C = new dlistEl;              // Tworzymy listę z wierzchołkiem v1
  C->v = v1;
  C->next = NULL;
  C->prev = NULL;

  for(p = C; p; p = p->next)    // Przeglądamy listę C
    for(i = 0; i < n; i++)      // Szukamy sąsiadów
      if(graf[p->v][i])
      {
        for(j = 0; j < n; j++) visited[j] = false;
        DFSaddCycle(p->v,i,p);  

  cout << endl;

  // Wyświetlamy zawartość listy C, czyli pełny cykl Eulera

  for(p = C; p; p = p->next) cout << setw(3) << p->v;

  cout << endl;

  // Usuwamy zmienne dynamiczne

  p = C;
  while(p)
  {
    p = C->next;
    remC(C);
    C = p;
  }

  for(i = 0; i < n; i++) delete [] graf[i];

  delete [] graf;
  delete [] visited;

  return 0;
}

And here is code in C:

#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable: 4996)

typedef enum { true = 1, false = 0 } bool;



struct dlistEl
{
    struct dlistEl *next, *prev;
    int v;
};



int m, n;                        // Liczba krawędzi i wierzchołków
char **graf;                    // Dynamiczna macierz sąsiedztwa
bool * visited;                 // Tablica odwiedzin

void addC(int x, struct dlistEl *p)
{
    struct dlistEl * r;

    r = (struct dlistEL *)malloc(sizeof(struct dlistEL));
    r->v = x;
    r->next = p->next;
    if (r->next) r->next->prev = r;
    r->prev = p;
    p->next = r;
}

void remC(struct dlistEl *p)
{
    if (p->next) p->next->prev = p->prev;
    if (p->prev) p->prev->next = p->next;
    free(p);
}

bool DFSaddCycle(int v, int w, struct dlistEl * & p)
{
    int u;

    visited[w] = true;            // Oznaczamy v jako odwiedzony
    addC(w, p);                    // Dodajemy w do cyklu
    p = p->next;                  // p wskazuje dodany element
    for (u = 0; u < n; u++)        // Przeglądamy sąsiadów w
        if (graf[w][u])
        {
            if (u == v)                // Cykl znaleziony?
            {
                addC(v, p);              // Zamykamy cykl na liście C
                do
                {
                    graf[p->v][p->next->v] = 0; // Usuwamy krawędzie cyklu
                    if (p->v == v) return true;
                    p = p->prev;
                } while (true);
            }
            if (!visited[u] && DFSaddCycle(v, u, p)) return true;
        }
    p = p->prev;                 // Z listy usuwamy w
    remC(p->next);
    return false;
}

int main()
{
  int i,j,v1,v2;
  struct dlistEl *C,*p;

  scanf("%d %d", n, m);         // Czytamy liczbę wierzchołków i krawędzi

  // Tworzymy tablice dynamiczne

  graf    =(char **)malloc(sizeof(char *)*n);
  visited =(bool *)malloc(sizeof(bool)*n);
  for(i = 0; i < n; i++)
  {
    graf[i] =(char *)malloc(sizeof(char)*n);
    for(j = 0; j < n; j++) graf[i][j] = 0;
  }

  // Odczytujemy definicje krawędzi grafu

  for(i = 0; i < m; i++)
  {
      printf("podaj def krawedzie grafu: \n");
      scanf("%d %d", v1, v2);
    graf[v1][v2] = 1;
  }

  C = (struct dlistEL *)malloc(sizeof(struct dlistEL));   
  C->v = v1;
  C->next = NULL;
  C->prev = NULL;

  for(p = C; p; p = p->next)    // Przeglądamy listę C
    for(i = 0; i < n; i++)      // Szukamy sąsiadów
      if(graf[p->v][i])
      {
        for(j = 0; j < n; j++) visited[j] = false;
        DFSaddCycle(p->v,i,p);  
      }

  printf("\n");

  // Wyświetlamy zawartość listy C, czyli pełny cykl Eulera

  for (p = C; p; p = p->next) printf("   %d", p->v);

  printf("\n");

  // Usuwamy zmienne dynamiczne

  p = C;
  while(p)
  {
    p = C->next;
    remC(C);
    C = p;
  }

  for(i = 0; i < n; i++) free(graf[i]);

  free(graf);
  free(visited);

  return 0;
}

When I try to compile the program written in C it says:

error: invalid application of 'sizeof' to incomplete type 'struct dlistEL'|

error: expected ';', ',' or ')' before '&' token|

error: invalid application of 'sizeof' to incomplete type 'struct dlistEL'|

Community
  • 1
  • 1
  • You probably need a `typedef struct`, to use it by name. – πάντα ῥεῖ Jun 20 '15 at 14:23
  • 2
    Do not generate your own `bool` type in C. Use `stdbool.h`. Do not cast to/from `void *` in C as used by `malloc()`& friends (differs from C++!). Do not disable warnings per file, but only where they actually appear (and only if well thought, not just as they annoy you). `sizeof(char) is _defined_ to be 1 by the standard; there is no use to get it. – too honest for this site Jun 20 '15 at 14:32

2 Answers2

3

I think, the problems are

  1. In your code

    bool DFSaddCycle(int v, int w, struct dlistEl * & p)
    

    you may want to change that to

    bool DFSaddCycle(int v, int w, struct dlistEl * p)
    

    There is no pass-by-reference in C. It's only pointers that you can make use of.

  2. You're mistyping the struct name.

     malloc(sizeof(struct dlistEL));  //uppercase
    

    should be

     malloc(sizeof(struct dlistEl));  //lowercase
    
  3. and finally, do not cast the return value of malloc().

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Is it struct dlistEL or struct dlistEl?

struct dlistEL /* ends with uppercase L */
struct dlistEl /* ends with lowercase L */
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    I don't mind @SouravGhosh ... but there is no need to summarize all the answers in one single answer. The format of Stack Overflow is good enough for several different ideas in several different answers. – pmg Jun 20 '15 at 14:34
  • sir, I also believed that previously, I used to link to the previous answers and append mine, but then some other's down-voted and told me "that's not a complete answer, you should write your own, if the other answer is deleted, nobody can know what you were pointing to." – Sourav Ghosh Jun 20 '15 at 14:38