(C language)why does this code fail to compile

  • Thread starter Linda8888
  • Start date
  • Tags
    Code
In summary,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?main.c includes <stdio.h> and <stdio.h> should be included before <HisStruct.h>, but main.c doesn't.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
  • #1
Linda8888
7
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
  • #2
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
  • #3
does main.c forgets to include <stdio.h>?
 
  • #4
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
  • #5
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
  • #6
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:

include guard code:
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
... your MyStruct.h code
#endif /* MYSTRUCK_H */
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
  • #7
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
  • #8
Thank you all. I got it!
 
  • Like
Likes Jarvis323

Question 1: Why is my code showing syntax errors when I try to compile it?

This could be due to a variety of reasons, such as missing semicolons, incorrect use of brackets, or using reserved words as variable names. Make sure to carefully check your code for any typos or mistakes in syntax.

Question 2: How do I fix "undefined reference" errors when compiling my code?

This error typically occurs when your code references a function or variable that has not been defined. Make sure to include all necessary header files and properly declare and define your functions and variables.

Question 3: Why am I getting "duplicate symbol" errors when compiling?

This means that there are multiple declarations for the same symbol in your code, such as a function or variable. Make sure to only declare and define each symbol once.

Question 4: How can I troubleshoot "segmentation fault" errors when compiling my code?

Segmentation faults occur when your code tries to access memory that it does not have permission to access. This could be due to an uninitialized pointer or accessing an array out of bounds. Use a debugger or print statements to track down the source of the error.

Question 5: Can I compile my code on different operating systems?

Yes, the C language is designed to be platform-independent. However, there may be slight differences in how your code is compiled and executed on different operating systems, so it is always a good idea to test your code on multiple platforms.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
6
Views
922
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
902
  • Programming and Computer Science
Replies
4
Views
736
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top