What are some good ways to structure a C program? I'm coming from an OO background and I can't wade through these thousands of pajeet videos that have flooded YouTube, just to find a few nuggets of good advice. Seriously, the search results for anything related to C are absolutely horrendous.
Define your data in data.h Define your methods acting on data in methods.h Include data.h before including methods.h in main.c
Brody Johnson
so C is an Indian-tier language?
Nolan Jones
OO works in C too If the struct is named x, then the functions will be like void x_init(struct x*) int x_do_stuff(struct x*) void x_free(struct x*) For ages, people turned to OO where it suited the problem well. C is pretty great because it doesn't force you, when the program fits another paradigm better. Actually I prefer C++ though, mainly because templates, but builtin OO support isnt bad either. They could work a bit on functional proframming tho. Anyways, this is the best in multi-paradigm languages, you can freely choose whichever fits the problem at hand the best.
Elijah Lewis
state machines
Elijah Wilson
If I pass a pointer of the struct to a function, can I just dereference it and modify the values inside of the function, or is this extremely bad practice? Didn't seem like another logical way to do it. I'd hate to do that in another language.
Thanks for the other tips. I guess I'll just put the structs and their related functions in a file each. I also believe OO is overkill.
Owen Clark
>If I pass a pointer of the struct to a function, can I just dereference it and modify the values inside of the function, or is this extremely bad practice? Didn't seem like another logical way to do it. I'd hate to do that in another language. Of course you can. If you don't want to let it modify, make it accept a const struct x*, that's what const is all about after all.
Daniel Bailey
Passing pointer is the most optimal way to modify a struct, since it only passes 4-8 bytes. As opposed to making a whole copy if the struct.
Juan Garcia
What about indexing into global array? is that as fast as using a pointer?
Josiah Campbell
Perfect, thanks lads
Cooper Sanders
stole my post from advent of code general, lil faggot
Jack Powell
obj files
Evan Wilson
oh, is it missing from there? sorry faggot
Parker Evans
This is a joke right?
Leo Reed
yet to see an indian C coder they are mostly doing java android c# and sometimes c++
Leo Martinez
piracy is stealing, you'll hear from my attorney
Ryder Nguyen
Anyone can answer this?
Isnt a[i] = *(a+i)?
Adrian Cruz
yes &a[i] is (a+i)
Ryder Gutierrez
.
Jace Hernandez
Not commercially, just the tutorials. It was probably the first programming language many people heard of, so of course there is an unending avalanche of shitty videos.
Adam Long
HolyC is actually an improvement over C.
Michael Butler
Yes thats literally what that operator does. Thats why you can go autistic and write i[a] and it will work the same
Jose Jenkins
Does this mean one should define their own constructors for structs? Is there a standard for approaching this?
Anthony Hill
but would it be faster to use indexes instead of pointers if you can store your structs in arrays?
What i mean is accessing the struct using struct A* a; and doing a.member = 2; vs. accessing it with mystructs[i] each time, mystructs[i].member = 2;
Jeremiah Robinson
Depends on the size of the structure. If the structure is small (2 pointers or less) and you don't need mutation, just pass by value. It'll probably be faster because you don't have to worry about aliasing.
Noah Lopez
(mystructs+i)->member = 2; might be slightly faster but not worth it
Jackson Powell
Its more of a convention, for where the initialization is non-trivial or opaque to the caller. It avoids a lot of problems. Say, you have a matrix structure, with members width, height, elemsize and p (for pointer). Its a lot cleaner to have a function, where you just give the width, height, elemsize, and it does the allocation on its own. Imagine the case when you add a new member, say comparor function ptr, because you want to have your matrixes sorted. Change the prototype of the constructor, and all outdated uses will be compiletime errors, instead of random crashes where you try to sort a matrix and comparor func is random memory junk.
John Lee
>don't ever define a variable in header files. >don't ever define a function in header files. (you can declare them, but the actual definitions must be in .c files).
>don't use global variables, if you have to, read the following; >if a function or global variable is specific to a single .c file, define it as static. it means no other .c file can access it, and compiler optimize it better (inline etc.). >only public (non-static) functions must be declared in header files. if you have a public global variable, define it in .c file, declare it as extern in header file. (so basically a .c file must be constructed as a single module).
>never include header files in header files. include them in .c files. the include order in .c file solves all the problems you envision.
i will not talk about practices of C as your question is only about structuring a C program, but you should read "practice of programming" by kernighan and pike anyway if you haven't already.
Gavin Davis
or you can define all your variables in one file, and all your and implementations in another file. Prove to me that this doesnt work.
Caleb Hill
works is not the question
Thomas Rogers
Thanks for this post, it was very helpful. Should structs go in header files? I will check out the book.
Aaron Robinson
What implementation? Defining means actually making it, like, without "extern". Did you mean declaring?
Christopher Stewart
yes
Lincoln Martin
>define all your variables in one file, >and all your implementations in another file ew
Andrew Ross
Instead of having to waddle through garbage you can just look in a single file, keep the principle of one function per task and you are good.
Longer compilation times because you need to create a massive .o instead of just recompiling the changes to smaller files and linking them.
Jose Harris
this is a good argument assuming you never have to make changes that might ripple through your program
Cameron Gutierrez
>not structuring your program in such a way that you never need to make changes which might cause a ripple through your program
Mason Jones
First of all, stop using youtube for programming guides, you fucking imbecile.
Thomas Campbell
impossible if you are trying to solve any real problems
Camden Hall
Recommend some good books that were written in the last 40 years then. Also, Google searches have the same problem, thousands of pajeet blogs top all of the results.
However C has headers, so use them. Header declares all functions and data structures; the source file implements them. Typically, you make a separate directory for your header files and your source files which makes it easier to make libraries or modules or whatever you call it.
As for how to write the functions, you can still write it as objects if you need to, but more often, you see it as functions that take objects as inputs instead.
John Bailey
So you assume he meant a single file for every implementation and a single file for every declaration? Because having these two split compiles faster in general. But this assumes you have a million files in a project.
Sebastian Peterson
>Should structs go in header files? it depends, if it's a private resource, let's say some handle, you can define in .c file and the public functions (interface) use void* for it.
void *create_xxx(void); int do_something(void *xxx); void destroy_xxx(void *xxx);
you can also use #define HXXX void *
this is especially useful for libraries. for instance, you may need to use variable types from 3rd party libraries in your struct. if you put structs in header file, users of your library should also find and include that 3rd party library headers to use your library, which is not nice.
Jackson Price
Thank you. Makes sense.
Adrian Torres
Just read some good C code, sooner or later you'll grasp how to make your programs efficient and well designed.
Connor Cox
This advice is bad. If you want an opaque handle, forward declare a struct then typedef it. This gives you strong typing. Using #define for a type is one of the most retarded things you can do in C.
Anthony Turner
thats a terrible practice because it bypasses type checking it can happen you will mistakingly use argument of different struct type and this solution will hapilly compile it
if you only use pointers to the struct in the header, you can simply declare "struct something;" in header file and define it "struct something{int lol};" in source file
Jaxson Murphy
you're right, typedef is the way to go, i was just being lazy. i also use enums and typedefs instead of macros where ever possible.
dude, this solves nothng, use forward declaration of the struct
do you even C?
Robert Sanchez
Any language that legacy software was written in is now considered Indian-tier; it doesn't necessarily reflect on the language itself.
Jacob Jackson
Imagine the smell.
Jack Gomez
You should avoid void* where you don't need it, too. Forward declared structs are more maintainable, less error prone and just plain more convenient.
Sebastian Lewis
So variables and functions that are used by multiple source files are declared in header files? I don't see what's so confusing about this unless I'm wrong.
Isaac Lopez
In most programming languages, the interface to a translation unit is generated automatically and you don't have to make a header file on your own.
Noah Cruz
>So variables and functions that are used by multiple source files are declared in header files? 100% exactly
Nolan Thompson
What the fuck ever? This is general knowledge everyone Cnile would know.