Discussion Overview
The discussion revolves around a programming issue related to the definition and instantiation of a structure in C, specifically concerning multiple definition errors encountered during compilation. Participants explore the implications of defining a structure in a header file and the correct usage of instances of that structure across multiple files.
Discussion Character
- Technical explanation
- Debate/contested
- Mathematical reasoning
Main Points Raised
- One participant expresses frustration over a linker error indicating multiple definitions of a structure instance named Sensor, despite believing it is only defined once.
- Another participant points out that the line struct sensor_data Sensor; creates an instance of the struct, which could lead to multiple definitions if included in a header file used across multiple source files.
- There is a discussion about the necessity of defining and instantiating structures in header files, with some participants suggesting that only the struct definition should be present in headers.
- One participant mentions needing the structure to be accessible by multiple functions defined in separate files, raising concerns about passing structures by reference and encountering syntax errors.
- There is clarification that passing structures by reference in C requires pointers, contrasting with C++ syntax.
- Another participant suggests removing the struct instantiation from the header file to avoid linker errors and proposes a method to create and use pointers to the structure.
- One participant describes their approach to restructuring the code by moving the struct definition and instantiation to the main function, indicating an attempt to resolve the linker error.
Areas of Agreement / Disagreement
Participants generally agree that the multiple definition error is likely due to the struct instance being defined in a header file. However, there is disagreement on the best approach to structure definitions and instantiations, with differing opinions on whether they should be included in headers or defined locally in source files.
Contextual Notes
There are unresolved issues regarding the proper handling of structure instances and the implications of using global variables versus local variables. The discussion also highlights the differences in syntax and behavior between C and C++ regarding passing structures.
Who May Find This Useful
Programmers working with C who are dealing with structure definitions, linker errors, and the nuances of passing data between functions may find this discussion relevant.