-1

Upon decomposition of my C program I've come across the problem of accessing the structures. Here's the code

storage.h

#ifndef STORAGE_H
#define STORAGE_H

#include "list.h"

#define MAX_TITLE_SIZE 1000

typedef struct Finances {
    int revenue;
} Finances;

typedef struct Website
{
    char title[MAX_TITLE_SIZE];
    int visitors;
    float average;
    Finances revenue;

} Website;


List * Strage_readFile(const char * fileName);


#endif

storage.c

#include "storage.h"



void blankfunc();

list.h

#ifndef LIST_H
#define LIST_H

#include "storage.h"

typedef struct List {
    struct List * next;
} List;

#endif

list.c

#include "list.h"


void blankfunc();

I'm getting this error

storage.h:22:1: error: unknown type name ‘List’
 List * Strage_readFile(const char * fileName);
 ^~~~

So, how can I organize the connection between these two headers, so that both Website and List structures were accessible in either headers?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

2

Mutual inclusion is your problem. storage.h must include list.h to compile, and list.h includes storage.h for some reason.

So simply remove the redundant include of storage.h from list.h

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I down voted as this should have been marked as an exact duplicate. it has been asked many times. And for someone with a reputation like yours, I'd expect it to be marked as duplicate and not answered. – Tony Tannous Mar 07 '17 at 21:59
  • 1
    @TonyTannous: that's not dreadfully constructive. It is harder to find the duplicates than it is to answer the questions. By a large margin. When 'they' provide a good duplicate tracker system that makes it easy for people to find duplicates, your viewpoint may be justified. Until then, it is very pedantically correct but not dreadfully practically correct. Also note that the duplicate proffered actually doesn't cover the case of "you don't need to include the header" as a way of breaking dependency cycles. – Jonathan Leffler Mar 07 '17 at 22:09
  • @JonathanLeffler I figured out very quickly what the problem is, googled for a `circular include` and the first result was the duplicate. Not that hard. For the second part it could have been a comment as well... I can't remove the downvote now unless @StroyTeller edits. But I still don't think this shouldn't have been answered in firstplace. – Tony Tannous Mar 07 '17 at 22:11
  • @TonyTannous - Not that hard for an experienced developer, or for a person struggling to learn the language? Because if you close as a duplicate, it should damn well be truly "identical" or general enough for the OP to figure out the problem. The one tacked up there is neither. – StoryTeller - Unslander Monica Mar 07 '17 at 22:13
  • @TonyTannous - And you shouldn't be shamed to taking back your downvotes. It's your opinion and it's a legitimate one. – StoryTeller - Unslander Monica Mar 07 '17 at 22:14
  • I have been here long enough to see many questions being closed as duplicates and this case wasn't any different. I am not ashamed, though I regret it, as it was a bit harsh, a comment would have been more appropriate . – Tony Tannous Mar 07 '17 at 22:14
  • @TonyTannous - Every case is different. Few topics have a true canonical duplicate. If you can suggest one that's really simple to follow, we can use that in the future. Or you could write it yourself. Phrase this question in as general terms as you can, and answer it yourself. Just note that you mean for it to be a canonical dup for people to hammer with (it's quite commonly done these days, actually). – StoryTeller - Unslander Monica Mar 07 '17 at 22:17
  • @TonyTannous - For example [this](http://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) and [this](http://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults) are what I consider canonical dups to close with. – StoryTeller - Unslander Monica Mar 07 '17 at 22:18
  • Perhaps I missed something but both links were to different question and answers. One regarding the cause of a segmentation fault and the other on the proper way to declare a two dimensional array and the disadvantages of OP example compared to an array residing in a contiguous memory block. – Tony Tannous Mar 07 '17 at 22:38
  • @TonyTannous - Those were examples of what a canonical duplicate *should* look like... not canonical dups to *this* question... – StoryTeller - Unslander Monica Mar 07 '17 at 22:39