0

I am stuck on (what i think is) a circular dependency problem in VS2017 coding in c.

I tried looking up the problem, and found a lot of similar problems on stackoverflow, but I can't seem to get my problem solved with those questions.

My code:

main.c

#include <stdio.h>
#include "travelers.h"
#include "trip.h"

int main(void) {

    int nTravelers = 0;
    int nTrips = 0;

    Traveler *travelersArray = (Traveler*)calloc(nTravelers, sizeof(Traveler));
    Trip *tripsArray = (Trip*)calloc(nTrips, sizeof(Trip));

    return 0;
}

travelers.h

typedef struct {
    unsigned int id;
    char *name;
    char *adress;
    char *residence;
} Traveler;

trip.h

typedef struct {
    unsigned int id;
    char *touringCar;
    char *destination;
    char *date;
    Traveler *travelers;
    unsigned int amount;
} Trip;

the travelers.c and trip.c files are only contain #include "travelers.h"/#include "trip.h"

The error occurs only in trip.h at Traveler *travelers;:

Error(s)

I don't know how to resolve this.

This looks like the same problem, but I couldnt translate it to my code.

Any help is apriciated.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 2
    Impossible to say from the information we have. Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – klutt Jan 04 '18 at 15:24
  • 1
    On a side note, [Please see this discussion on why not to cast the return value of malloc() and family in C..](https://stackoverflow.com/q/605845/2173917) – Sourav Ghosh Jan 04 '18 at 15:25
  • 1
    There is no circular anything here. Use appropriate header guards *and `#include` directives* in each header file. – n. m. could be an AI Jan 04 '18 at 15:25
  • The code you show here does not compile, but not for the reason you write but only because `nTravelers` and `nTrips` are not declared. – Jabberwocky Jan 04 '18 at 15:27
  • 1
    Side note: you also forgot `#include ` – Jabberwocky Jan 04 '18 at 15:28
  • 1
    @MichaelWalz .... which s/he would have noticed if s/he hadan't casted `calloc()` as not necessary in C. – alk Jan 04 '18 at 15:29
  • 1
    Also please refrain from posting images of text. – n. m. could be an AI Jan 04 '18 at 15:30
  • @alk correct, but not including `stdlib.h` is really an error nowadays. I get a warning `'calloc' undefined; assuming extern returning int`. – Jabberwocky Jan 04 '18 at 15:37
  • @MichaelWalz: Agreed. Still not everybody considers a warning (as you got) an "error". – alk Jan 04 '18 at 15:46
  • 1
    I rolled back your question to its (more or less) original state. As else comments/answers would not be understandable any more. – alk Jan 04 '18 at 16:31

2 Answers2

1

No cycles here.

If trip.c includes trip.h is shall also include travelers.h as its definitions (Trip) depends on the latter (Traveller).


Knowing this, one could include travelers.h into trip.h. Still, this complicates things, so it is a good idea to 1st of all add to every header so call header-guards, protecting against duplicate definitions on pre-processor level.

Doing so made the headers look like this:

travelers.h

#ifndef TRAVELERS_H
#define TRAVELERS_H

typedef struct {
    unsigned int id;
    char *name;
    char *adress;
    char *residence;
} Traveler;


#endif  // #ifndef TRAVELERS_H

trip.h

#ifndef TRIP_H
#define TRIP_H

#include "travelers.h"  // makes including it unnecessary where trip.h is included

typedef struct {
    unsigned int id;
    char *touringCar;
    char *destination;
    char *date;
    Traveler *travelers;
    unsigned int amount;
} Trip;


#endif // #ifndef TRIP_H
alk
  • 69,737
  • 10
  • 105
  • 255
  • ...and in the order of first `travelers.h` and then `trip.h`so that `Traveler` is known before it is used in `Trip`. – Paul Ogilvie Jan 04 '18 at 15:54
  • I implemented this in my code, including the changes for the calloc() casting, but it still gives me the same error. I edited the code in the question. –  Jan 04 '18 at 16:16
  • 1
    @NoëlVissers: Did you do a **re**build? – alk Jan 04 '18 at 16:25
  • @alk Im sorry. Totally forgot that. I usually dont program in c or in VS. But now it works. Thank you verry much sir! –  Jan 04 '18 at 16:29
0

As a remark, the error is caused by the typedef. C accepts opaque structs provided you do not need their implementation details:

a.h:

struct A {
        int aVal;
        const char * astr;
};

a.c:

#include "a.h"

const char *getAStr(struct A*a) {
        return a->astr;
}

b.h

const char *getName(struct B*);

struct B {
        int bVal;
        struct A *a;
};

b.c

#include "b.h"

const char *getAStr(struct A*);

const char * getName(struct B* b) {
        return getAStr(b->a);
}

main.c

#include <stdio.h>
#include "a.h"
#include "b.h"

int main() {
        struct A a = { 1, "foo" };
        struct B b = { 2, &a };

        printf("%d - %d : %s\n", b.bVal, b.a->aVal, getName(&b));
        return 0;
}

compiles and links without even a warning while in b.c nothing is known on struct A except that it is a struct.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252