Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are pointers and recursion looked upon as a complicated issues?

Recently I was reading about Article on Interviewing an Software Engineering Position by Joel and he mentioned about asking candidate about Recursion and Pointer's after some simple puzzles.

I wonder why Pointers and Recursion are considered to be complicated Issues ?

Update: What can be done to improve on Pointers and Recursion Skills, if I may say so ?

Thanks.

like image 795
Rachel Avatar asked Oct 07 '09 05:10

Rachel


People also ask

Why are pointers so hard to understand?

Proper understanding of pointers requires knowledge about the underlying machine's architecture. Many programmers today don't know how their machine works, just as most people who know how to drive a car don't know anything about the engine.

How are pointers used in recursion?

Create a "global" context in the caller and pass it to all recursive levels through a pointer parameter. That way all recursive levels will use the same "global" data, instead of instantiating that data locally again and again on each level of recursion.

What is recursion and why is it important?

Recursion is an important concept in computer science. It is a programming technique that involves a function repeatedly calling itself until it reaches a solution. A recursive function, then, is a function that calls itself.

Are pointers tough?

Pointers are arguably the most difficult feature of C to understand. But, they are one of the features which make C an excellent language. In this article, we will go from the very basics of pointers to their usage with arrays, functions, and structure.


7 Answers

Someone once said to me, and I agree - pointers are a simple concept but difficult to code, recursion is a difficult concept but easy to code.

Pointers can be tricky to code, because it might not be obvious where the problem is, or that there even is a problem - issues caused by pointers might not show up in the first or second or even one hundredth run, but then suddenly bam, you've got an issue. Debugging them can be very difficult as well.

Recursion is simple to code - just have the function call itself, and do something to keep track of where you are. The difficulty is in making sure you have a good enough understanding of all the possible paths your function could take, and making sure that it can always get itself out of a loop.

like image 88
ScottF Avatar answered Nov 10 '22 01:11

ScottF


They require keeping details about several layers of data. Several layers are more complicated than one layer.

A pointer is more compicated than a value variable. It is a variable by itself but it is used to hold an address of another variable. You need to have a grasp on the difference between the pointer value and the value of that other variable.

Recursion is more complicated than plain code with loops since you need to have a grasp on why and how the current call has been made, what other calls will be made, what will be the effect when the current call returns and how all this solves the problem at hand. That's complication. Add indirect recusrion to this (when A() calls B(), B() calls C() and C() calls A() again) and gets really interesting.

like image 45
sharptooth Avatar answered Nov 10 '22 00:11

sharptooth


Recursion is simple enough to be used in LOGO - a programming language similar to LISP designed to be easily used by young children. It's a fairly intuitive concept for many basic uses.

Pointers on the other hand, seem complicated to many programmers -- especially those that have never touched assembler. Especially confusing is the fact that C (and C++) basically treat arrays and pointers nearly interchangeably even though they are often represented by different data types.

For example, an array of pointers to 1D-arrays is dereferenced exactly the same in source code as a two-dimensional array even though they have completely different memory layouts and generate considerably different machine code when the dereferences occur.

like image 41
Adisak Avatar answered Nov 10 '22 02:11

Adisak


why Pointers ... are considered to be complicated Issues ?

There's actually a Joel article that slightly answers your question: in The Guerrilla Guide to Interviewing (version 3.0), he says (original emphases):

I’ve come to realize that understanding pointers in C is not a skill, it’s an aptitude. In first year computer science classes, there are always about 200 kids at the beginning of the semester, all of whom wrote complex adventure games in BASIC for their PCs when they were 4 years old. They are having a good ol’ time learning C or Pascal in college, until one day they professor introduces pointers, and suddenly, they don’t get it. They just don’t understand anything any more. 90% of the class goes off and becomes Political Science majors, then they tell their friends that there weren’t enough good looking members of the appropriate sex in their CompSci classes, that’s why they switched. For some reason most people seem to be born without the part of the brain that understands pointers. Pointers require a complex form of doubly-indirected thinking that some people just can’t do, and it’s pretty crucial to good programming. A lot of the “script jocks” who started programming by copying JavaScript snippets into their web pages and went on to learn Perl never learned about pointers, and they can never quite produce code of the quality you need.

That’s the source of all these famous interview questions you hear about, like “reversing a linked list” or “detect loops in a tree structure.”

Apologies for the big quote, but it's all there.

like image 38
AakashM Avatar answered Nov 10 '22 02:11

AakashM


Understanding of pointers requires a conceptual understanding of the memory architecture of your computer system: each address in numbered memory is a slot where you can put data, and some of the slots contain numbers of other slots. That's non-trivial. More importantly, it's not required for simple programs. Having that conceptual understanding and being able to solve problems with it shows in my opinion a desire to figure out what's really going on in a computer.

Further, to be fluent with pointers you need practice. Think of C puzzlers like: 'from the declaration below, describe the type of a in words'

int* (*)(int*[]) (a*[])(int *[]*, float[][]**);

What's required to answer that question quickly and with ease, or any other that deals with many levels of indirection? You need to have spent time to think deeply about what pointers are at the programming language level.

It's a similar story with recursion. To simply walk through a recursive function you've got to understand the call stack and how arguments are passed and returned. To master recursive functions you've got to be able to decompose a problem into smaller parts and solve each one piece by piece.

Fluency with recursion and pointers requires concepts that are the foundations for writing hard code. I believe the dedication required to master those concepts is a crucial ingredient in a good programmer. What's more, it's not too hard to show holes in someone's knowledge of concepts with a 10 minute programming test on a topic that requires them. Sound like great interview questions to me.

like image 27
Alex Avatar answered Nov 10 '22 01:11

Alex


I would say because a lot of students don't grasp programming fundamentals strongly enough before trying out pointers and recursion so they get confused too early.

Also, students like to influence other students into thinking a concept is harder than it actually is.

I was fortunate enough to learn pascal and C as my first programming language, so pointers and recursion was natural and didn't feel complicated.

like image 41
Andrew Keith Avatar answered Nov 10 '22 00:11

Andrew Keith


They are simple concepts, but they're easy to screw up.

That makes pointers and recursion a very suitable topic for programming puzzles and interview questions.

like image 27
Alterlife Avatar answered Nov 10 '22 02:11

Alterlife