I'm learning C for fun, but pointers, structs, and dynamic memory aren't clicking yet. Where should a beginner start, and are there free resources worth using? Is learning C still worthwhile with AI around?
How can I get comfortable with pointers and memory in C?
Answers
Start small and build up. Declare a few variables and arrays in main, change their values, then pass them to functions. Add a simple struct once you're comfortable grouping related values. You don't need dynamic allocation on day one.
For pointers, draw a box for a variable and an arrow from the pointer to that box. Try a tiny program with int x = 5 and int *p = &x. The pointer holds x's address, and *p accesses the value at that address. Change *p and print x to see the connection.
After that, try allocating space for an array with malloc, checking whether the result is null, using the array, and freeing it once when you're done. Trace which pointer refers to the allocation and don't use it after free. A debugger can help you inspect the values as you step through.
Reply to this answer
Beej's Guide to C is free and covers the language directly. CS50 is another free option if you'd rather have a broader, structured introduction to programming that includes C. Pick one and work through the exercises instead of just reading the explanations.
n.752+buttonferrule:
Would CS50 be suitable if C is my first language?
_r29+tinwicket.6:
Yes. It starts as an introduction to computer science rather than assuming you already know another language, though it covers more than C.
Reply to this answer
For fun? Sure, that's enough reason. C lets you explore how programs work closer to memory and the operating system. Whether it's the best tool for a particular project is a separate question. AI can generate code, but learning what that code does can be the interesting part.
Reply to this answer
Make a little sandbox and experiment. Write a function that takes a pointer and changes a variable, then try passing an array to a function. Predict the output before you run each version. When something surprises you, step through it in a debugger rather than changing five things at once.
C has a fairly small set of language features, but using them safely takes practice. Not understanding pointers immediately doesn't mean you're bad at it.
Reply to this answer
Add your answer
Answers are reviewed before publication.
_q43+hingequill.8:
Drawing the boxes and arrows before running the program can make it easier to spot where your mental picture differs from what the code does.