r/C_Programming • u/Feisty_War80 • Jun 19 '26
Question how do yall learn C 😭😭😭(rant)
Everything so far has been okay, but the second I started with pointers my brain just shut down, like whats with the * and & 😭 and after that storage classes dont even get me started on that
(YALL TY FOR THE RESPONSES IMMA TRY AND READ THEM ALL)
0
Upvotes
•
u/AutoModerator Jun 19 '26
Looks like you're asking about learning C.
Our wiki includes several useful resources, including a page of curated learning resources. Why not try some of those?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
26
u/MagicalPizza21 Jun 19 '26
It would help to understand the basic memory model of a computer program. Do you?
8
u/Za_Paranoia Jun 19 '26
One thing at a time, my friend. Pointers can be tricky in the beginning but will be your best friend in no time. It is hard for most people in the beginning.
7
u/Vergil_999 Jun 19 '26
once you internalize the idea that data is in memory which is an array of bytes, i think pointers are inevitable they were just concealed by other programming languages for simplification, but are necessary if you want control and precise data manipulation, it even appears more plainly at the assembly level, i know it looks scary especially if you came from a non CS background just for the sake of coding, but if you are so then why you chose C anyway ?
4
u/mohamadjb Jun 19 '26
I started learning Assembly language before C, and after studying addressing modes in assembly, pointers in c is much easier
You have to understand computer memory addresses before being able to understand c pointers
Also in assembly you've got labels, which are memory locations where you store data, and use that label as a c-pointer
Good luck, hope you understand them
3
u/leon_bass Jun 19 '26
Best to think of * and & as an operator
2
u/rickpo Jun 19 '26
It's the only thing in C that will take you more than an hour to learn. Keep at it. This time next week, you'll have an a-ha moment and it will suddenly all make sense.
2
u/Rabbitical Jun 19 '26 edited Jun 19 '26
Honestly once you see through it, memory management is really, really dead simple. When I was starting out I found it helpful to think of computer memory as just a library with a bunch of shelves with books on them. (Do kids even know what libraries are anymore?). But thats really all it is:
A library book is a single element of data. A pointer is just the location of a book in the library: what shelf, what position. You tell the computer to save this location of a book, rather than the book itself, by creating a variable *my_book_location. You can get the location of a book with &my_book. You can tell the computer to read or replace the actual book at a location by "dereferencing" with *my_book_location (the * symbol having different meaning here than when you used it in creating the location pointer).
If you're already looking at one book, you can move on to the next one by adding 1 to the pointer. Arrays are just pointers to the start of a shelf, the index is how far down the shelf to look. Doing pointer math, and arrays+index are somewhat interchangeable in this sense. With pointers or arrays it's possible to look right past the end of a shelf and to something you didn't expect if you don't keep track of how wide the particular shelf is you're looking at.
Pointers are typed, meaning they should be used only to point to a single type of item: pointers to magazines, pointers to fiction books, pointers to self help books, whatever. void* pointers are wildcards meaning they are a valid location of anything.
Storage classes are just different areas of the library off limits to different kinds of customers.
I'm sure everyone on here will come and "well acshually" me on the finer points of all these analogies but my point here is not completeness, just a starting point for someone who literally has no idea what any of it means I think it can be helpful, at least it was for me back in the day.
1
u/siddsp Jun 19 '26
If you're having a hard time with pointers, look up the indirect function on Excel. It's essentially that, but for memory addresses on your computer. * Has two meanings with pointers. When used in variable/parameter declaration or type definition, it means "pointer to". When used in an expression, it means "value of" (also known as dereferencing). & Means "address of".
So something like
foo(int *x)is a function that takes an "integer pointer to" something and calls it "x".foo(&a)passes the address of the integera.Hopefully that kinda makes sense?
1
u/Hyvex_ Jun 19 '26 edited Jun 19 '26
You have to go a bit at a time. One of the most mind blowing and eye opening thing is realizing everything is just data. From how I understand it, "&" gives you the memory address of the thing specified and "*" tells you/the computer that the variable stores the memory address and not a value.
When you do "*ptr", it dereferences a pointer and basically says "go to that memory address and grab the value". This is important because a pointer stores the memory address value, so accessing the pointer by itself only returns that address value.
1
u/buzzon Jun 19 '26
Pointers are notoriously step up in difficulty compared to topics before. Don't feel bad if you need to spend more time researching different sources two find one that works for you. And make sure to solve many assignments to make sure you understand.
1
u/Total-Box-5169 Jun 19 '26
There is something more fundamental you are missing right now, something you didn't learn as a kid as part of everyday life, otherwise it would simple to understand. You need to find what it is.
1
u/mjmvideos Jun 19 '26
Don’t think about * and & at first. Get comfortable with the ideas behind them. Think about values stored in memory. Think about variable names and what that means with respect to addresses and values. Think about how the value that some variables hold could be an address while others are just numbers that don’t have anything to do with memory addresses. How should I tell the two apart? When I use the name of a variable in an expression the computer uses the value of that variable. But what if I know that value is really an address and I don’t really want the address but I want the value that’s at that address. My variable doesn’t directly hold the value I want, it sort of points to the value I want. How could I easily tell the computer to go get that value? Maybe I could just put an asterisk in front of the variable name when I want it to follow the pointer. And how do I get an address into a pointer variable anyway? If I have a variable ‘b’ with value 20 and I want to make a pointer by storing the address of variable b in some other variable’c’ how can I do it? Maybe I could stick an ampersand in front of the name. What will make all this click for you is when you get a legitimate use for these features- passing addresses instead of copying large amounts of data, giving out an address so someone else can put a value in it for you. Play around with these on paper. Write numerical addresses with colons followed by some value. Write some variable names to the left of a few addresses. Make one of the variable values actually one of the addresses you used earlier. Now play around with writing expressions to get values and addresses out.
1
u/Liquid_Magic Jun 19 '26
This is what I like about programming in C using cc65 for the Commodore 64 and PET which are based on the 6502. You can easily learn all about the simple processor and how to program in assembler for it and then apply that to C programming and things make more sense.
Also the book: C Programming A Modern Approach by K. N. King is a great book and has a good walk through of things like pointers and addressing. Both the basics and in a later chapter the advanced stuff.
1
u/okimiK_iiawaK Jun 19 '26
A pointer is just the address of a variable! Instead of passing a value or getting a value back you pass the address of it, but in order to retrieve the value you have to dereference it.
Imagine a bank vault with lots of different boxes. Each box is numbered and can store something in it. For some things instead of passing along directly the contents of the box, you want to just say in which box the contents can be found. The “&” symbol allows you to get the number of the box of a variable you have created the “*” allows you to retrieve something from a box (except when creating the pointer which then it’s to let the compiler know it’s a box(address) and not the contents(variable/data)).
1
u/genafcvpxyr31 Jun 19 '26
But pointers are barrels of fun!!!
1
u/Proud_Initial_4285 Jun 20 '26
I was scared that this was my post and I forgot about it. RELATABLE HEYYY🙏🙏🙏
3
u/Virtual_Advisor_ Jun 19 '26
Read k&r
1
u/Daveinatx Jun 19 '26
I learned them using graph paper to write down variables, addresses, and values. If you use them along with your debugger and simple data structures, you'll learn fastest. It does take an epiphany, so give it a try a few times and then it will click. Remember, all computers really have are registers, memory addresses and values.
E: clarification
1
u/[deleted] Jun 19 '26
Honestly, you’re going to have trouble with pointers no matter what when you start. Just try to really understand pointer arithmetic, why some operations are defined while others are not, and how to write basic data structures and algorithms that use them. I got stuck on linked lists for so long until I realized I was allocating memory after moving into the tail node which was changing the address of the tail node every time.
Theres gonna be stretches where you learn so little in a long stretch of time, but also where you learn so much in such a short amount of time once you get the ball rolling. Keep programming, that’s the golden rule that BK emphasizes.
1
u/thank_burdell Jun 19 '26
1
u/Cylian91460 Jun 19 '26
Internet
Also I think your confusing c and c++
1
u/Computerist1969 Jun 19 '26
It's been 40 years since I learnt so I can't tell you how. I can tell you that I'm pretty sure it took a bit before I could deal with them confidently, though I did do 68000 assembly language beforehand.
1
u/thank_burdell Jun 19 '26
a pointer is just an address of something in memory:
as shown above, the & operator gives you the address of whatever comes after it. The * operator (in this context) is for dereferencing a pointer, or in other words accessing the contents of the address pointed to by that pointer.
1
u/matt9k Jun 19 '26
One of the most common mental models for a computer is a Turing machine. This means you can imagine your computer’s memory as being like a long strip of tape that you can write things on, and erase them later. Every “place” in memory is like a point along the tape.
When you store a variable, like `int x = 5;` that’s like writing on the tape. Later, if you use that variable, it’s like scrolling back to that part of the tape and reading what you wrote there. So using x tells the computer to go back to the part of the tape that it used to write x to, and read the number written there, in this case 5.
The program knows where on the tape it wrote x to. It has to know, so it can know where to scroll back to later, to get the value. What if you want it to tell you where that is? That’s when you’d use the address operator, `&`. Using `&x` tells the computer to reveal where along the tape it wrote x to.
On the other hand, sometimes a function will give you a pointer (in other words an address, or a point along the tape). You don’t know what’s written there, only where it is. Let’s imagine we have a pointer called `p` that points to something unknown. What if you want to see what value is actually written at that point along the tape? That’s when you’d use the dereference operator, `*`. Using `*p` tells the computer to scroll to the address called p and read what’s written there.
These are sort of opposites. If you wanted to be kind of dumb about it, you could write x as `*&x`. This means “take x, give me its address, then read the value at that address.” This is the same as just using x.
This is all pretty useless if you’re only using primitives like ints and chars directly. But it’s extremely useful when using structs
1
u/emmowo_dev Jun 19 '26
pointers literally point to another thing. If you modify its value, it effects the original object. ANY pointer itself is a big number (you can prove this using sizeof on a char vs a pointer) that corresponds to where it is, and your compiler will assume it has to read whatever is at where it points to.
This also means that if the original object is something that gets cleared from the stack (basically, it was created in a function that ended), that object is now pointing to random memory.
"Arrays" are really just pointers, and the array index
[]is actually the pointer's address + the index as an offset. so in an int array,i[1]is basicallyaddress + sizeof(int) * 1So, remember where your pointers are pointing to, and when they'll be potentially erased. Or, completely ignore this fear by
malloc'ing memory before you realize that it actually did nothing to the memory itself and you just created a new form of PRNG.1
u/EducationalTackle819 Jun 19 '26
& gives you the address a value is stored at, * takes an address and gives you the value at that location.
Imagine you want a function called tryIncrement that you pass an integer to and it attempts to increment it given some conditions and returns a flag indicating whether or not it incremented the value.
If you were to pass the integer as an ‘int’, it would get passed by value and the incrementing would not be reflected on the value the caller wanted to increment. If you pass a pointer to the int ‘int*’ the function can increment that actual value the caller is working with
‘’’c
int val = 1;
// val would still be 1 even if successful
int success = tryIncrementByVal(val);
// val will correctly increment
int success2 = tryIncrementWithPointer(&val);
‘’’
1
u/computermouth Jun 19 '26
Just make stuff bud, you'll get there. Making small games with raylib was how I learned pretty much
1
u/Fobioman00 Jun 19 '26
Inizia a ragionare con l'idea che una variabile tipo int* a deve contenere un indirizzo di memoria &. Se passi a in una funzione passi sostanzialmente &, se invece passi *a stai passando ciò che è contenuto dentro l'indirizzo di memoria (&). Se poi devi usare un secondo elemento (in ordine di gerarchia) dovrai usare più dereferenziazioni.
1
u/grimvian Jun 20 '26
& makes me think of where is it?
* and what's it?
1
u/SmokeMuch7356 Jun 20 '26 edited Jun 20 '26
First, take a deep breath and relax. Pointers really aren't that difficult to understand, but they're not wholly intuitive, either.
A pointer is just a way to access an object or function indirectly - we use them when we cannot (or don't want to) access something by its name. Either the name isn't visible to us, or the thing doesn't have a name (such as dynamically-allocated memory), or we want to refer to different things at different times with the same expression.
If you've worked with arrays at all, you've already seen a form of indirection in action with the subscript operator -
a[i]can designate different objects based on the value ofi.1The most common use case for pointers is to allow functions to write to their parameters. C passes all function arguments by value - when you call a function, each of the argument expressions is evaluated and the result is copied to the function's formal arguments. Here's a short example:
Output:
xandaare different objects in memory,2 so any updates toaare not reflected inx.foocannot write a new value toxdirectly becausexis not visible outside the body ofmain. If we wantfooto write a new value tox, we must pass a pointer tox:Output:
xandaare still different objects in memory. We're still evaluating the function argument expression and copying the result toa. However, instead of evaluating the expressionx, we're evaluating the expression&x; the unary&operator yields a pointer to the operandx.Instead of storing the value of
x,anow stores its address. The expression*aacts as a kinda-sorta alias forx; the unary*operator dereferencesa, allowing us to access the memory forxindirectly:This is important:
*adoesn't just yield the value stored inx,*aisx. Any operation that writes a new value to*ais writing a new value tox.The second-most common use case for pointers is tracking dynamically-allocated memory. C doesn't have a way to map dynamic memory to a variable name at runtime; instead, the library functions
malloc,calloc, andreallocall return pointers to that memory:The
datavariable is a separate object from the memory it points to:Again, we're using a pointer because we don't have a way to access that memory directly. We have to dereference
datato access that memory. Since the array subscript operator[]implicitly dereferences a pointer, we can just to things likeetc.
Basic pointer declaration rules:
a[i]is defined as*(a + i)- given a starting addressa, offsetielements and dereference the result.aandxdesignate different objects in memory.0
u/[deleted] Jun 19 '26
[deleted] — view deleted comment
1
u/[deleted] Jun 19 '26
[removed] — view removed comment
1
u/[deleted] Jun 19 '26
[removed] — view removed comment
1
u/[deleted] Jun 19 '26
[removed] — view removed comment