would here anybody like to suggest some good books on pointers ? Actually some of my friends often say me that pointer is hard , too hard(however i've not started learning pointers , so i can't say). And i'd like to make the pointer concept very clear , so please recommend some good books particularly targeted to Pointers so that i can learn some deep aspects of the pointer.

thanks.

Actually some of my friends often say me that pointer is hard , too hard(however i've not started learning pointers , so i can't say).

I strongly believe that the majority of what makes pointers "hard" is people hyping them as such. The concept is very simple, and the syntax is equally simple. You can get lost in a mess of indirection, but that's more a matter of design than pointers being "hard". You can just as easily make a mess of if..else statements, yet those aren't hyped as being unusually difficult to grasp and use.

so please recommend some good books particularly targeted to Pointers

I'm not aware of any at all, much less any good ones. There are tutorials online though. I think this one is a good intro, but I'm a bit biased.

Edited 3 Years Ago by deceptikon

i guess that they think pointer as very hard concept because pointer has a lot ways to access different values and different address in different different form.

one more reason is that pointer is applicable to a lot concepts(even we can access array through it, Memory allocation and so on). And i think if i grasp all these concepts then it'll be easier for me and i'll write such a tutorial so that nobody will hesitate about pointers.

Also ,i look forward to your tutorials on pointer .I hope you will write tutorial on pointer for the beginners like me free time.

Thanks.

Pointers can be explained pretty fully in just a couple of sides of paper. A whole book on them would be enormous overkill. They are very simple. The problems with them tend to come when people try to explain them with bad metaphors involving streets and house numbers, or envelopes and letters, or some other such unnecessary analogy that serves only to confuse.

I wrote down what I usually say about pointers here: http://www.cplusplus.com/articles/EN3hAqkS/

Edited 3 Years Ago by Moschops

Comments
cplusplus.com is on my "always open tab" set. :-)

The saying I came up with about pointers is this:
"Pointers: Beginners hate them; experts hide them."

The thing that makes pointers "hard" for some is that they are too simple, ironically. A pointer is nothing more than a number (integer) that uniquely identifies a location in memory (an address). That's all they are, nothing more. It's what they are not that is complicated for some to deal with, for example:

  • Pointers do not own the memory at the address, i.e., having a pointer to a location does not guarantee that that memory will remain valid.
  • Pointers do not always have a valid address value, i.e., they can point anywhere, including arbitrary or invalid addresses.
  • Pointers do not copy the pointed-to memory when they are themselves copied.
  • Pointers do not know what they are pointing to (nowhere, start / middle / end of array, stack / heap memory, etc.), and there is generally no way to find out.
  • Pointers are not managed by a garbage collector.

Beginners, especially those who come from a "higher-level" language where pointers are completely hidden, make mistakes, involving pointers and memory management, precisely because they assume one or many of the things listed above, or at least, they code as if to assume them, or, they are just really careless with pointers out of a habit they have developed in their "native" language. That's why beginners hate pointers.

Experts (that is to say, people with more experience in C++) deal with pointers by understanding that pointers alone do not carry much semantics and behavior (i.e., the things listed above that pointers do not carry / convey). So, if you want any or many of the things listed above, you write or use a class that will protect or enforce the specific semantics or behaviors that you want, while hiding away the pointer(s). And for this reason, you will rarely see pointers in interfaces (e.g., as function parameters) in modern C++ libraries (note: some older libraries do have them, especially pre-2000-ish libraries).

As far as having any kind of book that demystifies pointers, I can't really think of any. I would imagine that any decent C++ programming book must have at least a chapter about how to deal with pointers. The C++ Primer (5th Ed.) (Lippman, Lajoie, and Moo) has a chapter on dynamic memory, and several sections that deal with pointers in specific contexts, that should be far more than you need to understand them fully.

I'm looking at my copy of the ARM (Ellis and Stroustrup, Annotated Reference Manual for C++) now...

Ok. Can't find my ARM - it's probably at work, but now I'm looking at Stroustrup's C++ Programming Language, Second Edition.

He has several sections on pointers, but the short story is that they are variables that contain the address of the type they are a pointer to. IE,

int i = 0;
int* pToI = &i;

So, if you increment i (++i for example), then *pToI as well as i == 1. The * operator on a pointer to a type results in a reference to that entity (i in this case). In any case, you can redirect pToI to another integer and it will no longer be associated with the original variable. Example:

int i = 0;
int* pToI = &i;
int ii = 100;

++i;
++ii;
Now, i and *pToI == 1

pToI = ⅈ
Now, ii and *pToI == 101

Confused yet? Good, then you are on your way! :-)

thanks everyone for contributing to this thread.Now a hope raise inside me that whenever i'll start learning pointer then i'll not get more problems as other gets.i will not hate pointer.

@rubberman,
thanks for little nice explaination but thats not all about pointers[according to one of my friends, he says:
(pointer to array, dynamic memory , pointer to function , pointer to pointer ,types of pointers like near pointer , far pointer etc) are the other aspects that make the pointer hard.]

i think so.

Edited 3 Years Ago by Learner010

,types of pointers like near pointer , far pointer

Those are obsolete -- no longer used by modern compilers. You will only run into those if you use Turbo C or some other 16-bit MS-DOS compilers.

Otherwise, yes, pointers can become very difficult and confusing even to old programmers.

(pointer to array, dynamic memory , pointer to function , pointer to pointer ,types of pointers like near pointer , far pointer etc) are the other aspects that make the pointer hard.

  • Pointer to array: doesn't change the rules
  • Dynamic memory: doesn't change the rules in terms of pointers, dynamic memory is a different thing that happens to make use of pointers. Are we now saying that pointers are hard because of anything under the sun that might use them? If so, then any fundamental feature of C++ is "hard".
  • Pointer to function: actually changes two rules, they can't point to null, and they can't be generalized with void*. Pointers to functions are actually simpler than object pointers.
  • Near and far pointers: died with DOS.

The hardness of pointers isn't about pointers, it's about wrapping your head around a design that uses them. For example, linked data structures can use a lot of pointers that can be linked together in confusing ways. Or they can use a lot of pointers linked together intuitively. If you write spaghetti logic, it won't matter if you use pointers or not; the logic will be hard to manage.

Pointer to array: doesn't change the rules
Dynamic memory: doesn't change the rules in terms of pointers, dynamic memory is a different thing that happens to make use of pointers. Are we now saying that pointers are hard because of anything under the sun that might use them? If so, then any fundamental feature of C++ is "hard".
Pointer to function: actually changes two rules, they can't point to null, and they can't be generalized with void*. Pointers to functions are actually simpler than object pointers.
Near and far pointers: died with DOS.

i think that these replies exciting me to learn pointers very soon.i hope no doubt will be there for me if i grasp it completely.

thanks all of you for enriching this thread.

This article has been dead for over six months. Start a new discussion instead.