31 ms·
How do you keep your sanity while using pointers in C?
by csours 3y ago
How do you keep your sanity while using pointers in C?
- zabzonk 3y agoi've taught c and c++ extensively commercially, and i really can't remember anyone having real problems with understanding pointers, despite some trepidations they might have had when the topic was first introduced.
- csours 3y agoI understand pointer operations just fine. It's very hard for me to keep up with what is happening line to line. So, do people use consistent naming standards? Comment every line? Look at the assembly language produced by the compiler?
- porridgeraisin 3y agoInterpreting your question the best I can: There are usually patterns that you match the code you're reading to. For example, the simple pattern of int f(int x, int y, int len, int *z); and you immediately think "oh ok, they're doing something with x and y, and z is the out parameter. The fn is also likely going to allocate". Usually it matches what you see in the function body and it's all good. In (hopefully) rare cases you view it with such a lens and something doesn't make sense and you have to scrutinise the code for what it's actually doing. This is all without variable names. Proper variable names makes it much more straightforward to read e.g calling the function vec_add
- zabzonk 3y agoi'm not sure i understand your problem - why do you find understanding a basically sequential program so hard?
- ranger207 3y agoValgrind
- ordu 3y agoDon't use pointers to functions, or if you must then hide them behind typedefs.