(C language)why does this code fail to compile

  • Context:
  • Thread starter Thread starter Linda8888
  • Start date Start date
  • Tags Tags
    Code
Click For Summary
SUMMARY

The code fails to compile due to multiple inclusions of the same header file, specifically "MyStruct.h", without proper include guards. This results in redefinition errors. The solution is to implement include guards in header files, which prevent the same file from being included multiple times. The standard practice involves using preprocessor directives like #ifndef, #define, and #endif to encapsulate the header content.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with header files in C
  • Knowledge of preprocessor directives
  • Experience with struct data types in C
NEXT STEPS
  • Learn about C preprocessor directives and their usage
  • Research how to implement include guards in C header files
  • Explore common compilation errors in C and their resolutions
  • Study best practices for organizing C code and header files
USEFUL FOR

C programmers, software developers, and students learning about header file management and compilation issues in C.

Linda8888
Messages
7
Reaction score
1
TL;DR
Is it because both the variables of MyStruct in HisStruct.h and HerStruct.h, and the variable of int in MyStruct.h have the same name: 'a'
C:
[B]// MyStruct.h[/B]
typedef struct {
  int a;
  float b;
  char c;
} MyStruct;[B]// HisStruct.h[/B]
#include "MyStruct.h"

typedef struct {
  MyStruct a;
  int b;
} HisStruct;[B]// HerStruct.h[/B]
#include "MyStruct.h"

typedef struct {
  MyStruct a;
  float b;
} HerStruct;[B]// main.c[/B]
#include "HisStruct.h"
#include "HerStruct.h"

int main() {
  HisStruct foo;
  HerStruct bar;

  foo.a.a = 1;
  bar.a.a = 2;

  return 0;
}

[Code tags inserted by a Mentor]
 
Last edited by a moderator:
Technology news on Phys.org
Linda8888 said:
Is it because both the variables of MyStruct in HisStruct.h and HerStruct.h, and the variable of int in MyStruct.h have the same name: 'a'

The code itself is fine, but there is another problem. Can you work out what happens in the start of main.c just after the includes? Do you know the standard practice for making header files?
 
  • Like
Likes   Reactions: Linda8888
does main.c forgets to include <stdio.h>?
 
Linda8888 said:
does main.c forgets to include <stdio.h>?
No, that is not the problem.

An #include work by literally including the content of the given file at the point where the include is, meaning you will get same result if you put all the content yourself into one file (e.g. main.c). If you replace all includes with their respective content for your code snippet it should be more apparent what the problem is.

If the problem still escapes you then focus on how many times MyStruct.h gets included. Is it more than one? If so, why is that a problem? Are you aware of how to guard against the this include effect?
 
  • Like
Likes   Reactions: Jarvis323, Linda8888 and FactChecker
Linda8888 said:
Summary:: Is it because both the variables of MyStruct in HisStruct.h and HerStruct.h, and the variable of int in MyStruct.h have the same name: 'a'

C:
[B]// MyStruct.h[/B]
typedef struct {
  int a;
  float b;
  char c;
} MyStruct;[B]// HisStruct.h[/B]
#include "MyStruct.h"

typedef struct {
  MyStruct a;
  int b;
} HisStruct;[B]// HerStruct.h[/B]
#include "MyStruct.h"

typedef struct {
  MyStruct a;
  float b;
} HerStruct;[B]// main.c[/B]
#include "HisStruct.h"
#include "HerStruct.h"

int main() {
  HisStruct foo;
  HerStruct bar;

  foo.a.a = 1;
  bar.a.a = 2;

  return 0;
}

[Code tags inserted by a Mentor]

Welcome to PF, Linda. Please enclose your code in code tags (we have done that for you in your original post). Do it like this (but without the spaces):

[ code ]
<your code goes here>
[ /code ]

:smile:
 
  • Like
Likes   Reactions: Jarvis323, Linda8888 and Vanadium 50
This is a common problem with include header files. They may be included all over the place, but their contents must only be defined once. The solution is to surround the contents of the header file with something called an "include guard" in your header files:

[CODE lang="c" title="include guard code"]#ifndef MYSTRUCT_H
#define MYSTRUCT_H
... your MyStruct.h code
#endif /* MYSTRUCK_H */[/CODE]
It is standard to make MYSTRUCT_H all upper case and to put the comment on the endif line so you know what block of code is ending.
You should get used to doing this in all your header files.
 
  • Like
Likes   Reactions: Jarvis323 and Linda8888
Hi Linda, just a friendly tip:

In general, when a program fails to compile or run, it's very helpful to tell us the exact error message(s) that you get. :smile:
 
  • Like
Likes   Reactions: Jarvis323, Linda8888, Vanadium 50 and 1 other person
Thank you all. I got it!
 
  • Like
Likes   Reactions: Jarvis323

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
Replies
7
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 39 ·
2
Replies
39
Views
5K