Hello, I'm working on a school project where my assignment is to write a c library that generates a latex pgfplot file with a minimal amount of user input. To keep things simple for now I'm limiting myself to a bar graph. The user would then just have to provide some parameters and the data. My professor suggested using a system of nested structs to store the strings that need to be written into the latex file. Listening to him it all made sense but I don't understand how a c function would be able access the members in the structs without hard coding every member name into the write function. It doesn't look like c has a native way to effectively "loop" through the members of a struct like a object oriented language. I found some possible solutions like function mapping, but it doesn't seem like any of them have the flexibility my professor envisioned.
Any help or ideas would be appreciated. Thanks!
#C library for creating .latex pgfplot file with structs
10 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
C library for creating .latex pgfplot file with structs
sounds like a challenging assignment
Yeah, I am pretty sure your professor just expects you to hardcode the struct field names, since C doesn't have any way to loop through them. If you were writing C++ there may be a way, though.
Even if you were able to loop over the fields that likely wouldn't be overly helpful, since you'd likely want to treat fields differently based on the name anyways, and then you'd basically be adding a bunch of if-statements for no real gain over just simply hardcoding
yeah I chose it thinking it sounded simple, now I'm not so sure 😂
Thanks for the response. In his example my prof also included member pointers to other structs with the idea that the write function could travel through the structs kinda recursively. I also can't see how that would add modularity to the library because I would still have to hardcode the logic after 'stepping in'.
@agile pewter Has your question been resolved? If so, type !solved :)
You could definitely make a separate write function for some or each of those member pointers to other structs, but my words of wisdom is that you should only abstract (complicate) things when it proves to make your life easier, rather than doing it just cause you reckon it might help later :)