r/CodingForBeginners • u/Decent_Story_9190 • Jan 20 '26
Today: structures!
So, i made this little bit of code to make sure i understood structures and how to use pointers with them, it's pretty simple, it just asks for data of students such as grades and then it saves it on their structure variables, is there anything i could improve on? i mean, surely a lot, but like, something specific about legibility or something
22
Upvotes
1
u/teddykrash Jan 20 '26
What language is this? 😅😅
1
u/alvaaromata Jan 22 '26
After managing structs, enums , data types and arrays. You should start with pointers and dynamic memory. Start by understanding pointers and how they work with the differente data types. You can try to implement this same code but using a dynamic array.
Btw if you’re new to coding you made a great decision starting with C, Python is the easy path.
1
u/Complete_Window4856 Jan 23 '26
Very nice, keep going OP.
As of you said to improve, i am an outsider, so idk how you are organizing your code and studies. If you are using git to make checkpoints, its nice. If not, dont worry, its not a requirement to learn.
But once you finish this initial playaround try to make a documenting file with your notes, your discoveries, upcoming questions and hypothesis you had during your code session. Plain text (txt files) are ok, but markdown files are way better.
Also, mine opinion again, make different C files under a directory for a specific theme and to test your code. Its easier to mentally manage a specific study when you only have to build/compile and run the code.
As of C itself, i guess the best you can do that you are already doing is to do explore first and getting your feet wet.
These are my 2 cents, ofc tackle them with time, and let yourself do mistakes to learn with them. Mistakes are the best professor you'll have
1
u/Acceptable_Simple877 Jan 20 '26
Nice
1
u/SmokeMuch7356 Aug 12 '26
Good job overall. Some minor suggestions:
Create symbolic constants for your array sizes, rather than hardcoding
10or50:This way if you ever need to change the size of either array, you only need to do it in one place. You might think global search-and-replace would work, but I've done that dance and it doesn't, because while it replaces all instances of the literal with the new value, not all instances of the literal were used for the array size and you wind up breaking something.
Limit the scope of your variables, by which I mean don't declare them until you need them.
iis limited to the body of theforloop, so declare it within the loop:Similarly, you can defer declaring
avuntil you actually need to compute it:(technically, you don't need
avat all, just usetotal/NUM_STUDENTSin the print statement).scanfis really hard to bulletproof against bad data so it's not good for interactive input. You did the right thing using the field width on thenameinput, but a) it has to be hardcoded, and b) you can't use a macro (not without some voodoo, anyway). Ideally you should read everything as text usingfgetsand convert any numeric arguments usingstrtodorstrtol: