(C language)why does this code fail to compile

  • Thread starter Thread starter Linda8888
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion centers on issues related to header file inclusion in C programming. The primary concern is the potential for multiple inclusions of the same header file, specifically MyStruct.h, which can lead to compilation errors. The participants clarify that including the same header file multiple times without proper safeguards can cause conflicts, particularly with variable names. The recommended solution is to implement include guards in header files to prevent this issue. An example of an include guard is provided, emphasizing the importance of using them consistently to ensure that header file contents are only defined once. Additionally, it is noted that providing specific error messages during compilation can greatly assist in troubleshooting.
Linda8888
Messages
7
Reaction score
1
TL;DR 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]
 
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 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 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 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 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 Jarvis323, Linda8888, Vanadium 50 and 1 other person
Thank you all. I got it!
 
  • Like
Likes Jarvis323
Back
Top