(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

Discussion Overview

The discussion revolves around a C programming code snippet that fails to compile. Participants explore potential reasons for the compilation failure, focusing on header file inclusion practices and variable naming conflicts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • Some participants suggest that the issue may arise from naming conflicts, specifically that both HisStruct and HerStruct contain a variable named 'a' from MyStruct.
  • Others point out that the code may have issues related to the inclusion of header files, questioning whether MyStruct.h is included multiple times and how this could affect compilation.
  • A participant emphasizes the importance of using include guards in header files to prevent multiple definitions.
  • One participant notes that the absence of in main.c is not the problem, indicating that the issue lies elsewhere.
  • Another participant encourages providing exact error messages when encountering compilation issues for better assistance.

Areas of Agreement / Disagreement

Participants express multiple competing views regarding the cause of the compilation failure, with no consensus reached on a single issue. The discussion remains unresolved as various potential problems are identified.

Contextual Notes

There is a lack of clarity regarding the specific error messages encountered during compilation, which could provide further insight into the problem.

Who May Find This Useful

Readers interested in C programming, particularly in understanding header file management and common compilation issues, may find this discussion relevant.

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