ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Originally posted by exvor So C programmers what is your opinion on witch of these 2 concepts in C
programming are harder to understand for a learning programmer.
pointers or Recursion
I'm having issues being able to understand recursion my self but pointers
are equally challenging.
I always though recursion was the harder concept. Pointers always seemed to be very natural to me. I started out programming on a C64, though, and all the PEEK / POKE commands you had to do gave you a better understanding of the physical machine. Since pointers are a concept that basically maps to the hardware (putting issues like memory segmentation, virtual memory, paging, etc. aside for the moment), I guess that is why I felt they were easy to understand.
Recursion is a concept you see in lots of languages and in mathematics, too. To me, it is the "inside-out" way of looking at an iterative problem. However, there are situations where it is much easier to read a recusive program instead of an iterative program where you must maintain your own stack.
In my opinion, pointers are much more important and should be focused on more when you're learning. Recursion is nice and if you keep it in the back of your mind, it can pop up as an obvious clean solution to certain problems. But pointers need to be mastered, you can generally get by as a C/C++ programmer without using recursion, but you certainly can't do any useful tasks without a decent knowledge of pointers. Heck I dont know of any API's that don't return pointers to structures/objects, and expect the same as parameters.
When I was learning I probably had an average amount of frustration with pointers, but I found they did come to me and I feel I understand them very well now. When I was in school my teachers focused on drawing memory diagrams and I would turn around and recommend this practice to everyone now. Recursion came a little slower to me. I'm working on walking directory trees using recursion, because in this situation recursion seems more natural. This is what I mean by learn "what" it is early in your learning days, then keep it in your mind until it springs up as a good solution to a problem. It was a while before I saw a good problem for recursion to solve. I think they told me in school that everything you can solve recursively you can solve iteratively.
Pointers aren't a concept, they're a fundamental part of the language which you must master in order to write correct code. Recursion is merely a programming technique which the design of C facilitiates particularly easily).
Yeah, pointers are more important to understand, but the question was whether pointers or recursion was harder to understand. I personally love pointers, but to me, recursion is a much easier concept to understand.
I guess I would say recursion is easier, because its so self-contained. If you're recursive function is acting up, you know where to immediately look. Tracing memory leaks due to pointers being used improperly takes a significant amount of time. So even though everyone "knows" pointers, they still can be mishandled and time must be wasted finding the root of the problem. I'm sure we've all had our share of segfaults
Originally posted by tuxdev Yeah, pointers are more important to understand, but the question was whether pointers or recursion was harder to understand. I personally love pointers, but to me, recursion is a much easier concept to understand.
I'm not sure why people find pointers difficult to understand. They seem pretty straightforward to me. I come from an assembler background (actually hand-crafting machine-code back in the 70s!) so I got to understand what goes on at the machine level early on. Every language I've used since then has been "high-level" by comparison! Drawing pictures helps, I find, when explaining pointers to beginners.
I did once spend 20 minutes trying to explain a two line recursive function (not mine) to a maintenance programmer who just couldn't get it - in the end I waited 'til the coder left the project, raised a defect and "fixed" it so it worked efficiently without recursion (the entry and exit RAS overhead on the function really screwed performance with recursion).
Pointers are a bit of a problem in "C" because you have to use them so much. The original language does not have the concept of var or const parameters, and some compilers were quite loosy-goosy about what strangeness they would accept without saying anything... strangeness that, of course, was a bug.
When explaining a pointer to my classes, I always stressed that what it is is just an ordinary integer. What's different about it is what it does: namely, it contains an address. Therefore you can refer either to "the value of the integer that is in this pointer variable," or you can refer to "the whatever-it-is that presumably is to be found at that address." The choice is yours, and you'd better keep them straight. And I tell them that NULL corresponds to the special-value zero. Then, I just remind them to "draw a picture, just like I do."
Recursion has never been a problem. People just assume that a function should be able to call itself, if it wants to. I just have them write a little program that calls itself endlessly, so they can experience for themselves the totally-bogus error messages that usually pop out as a result.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.