maniac said:
Hey guys, I'm new here, just a simple question.
I'm learning to Program in C, and I was recommended a book called,
"Mastering C Pointers", just asking if any of you have read it,
and if it's worth the $25USD.
I am given to understand that the book is firmly rooted in MS-DOS, which
makes it unsuitable for /general/ understanding about pointers.
I'm just looking for a book on Pointers, because from what I've
read it's one of the toughest topics to understand.
Actually, pointers aren't really that hard to understand.
Every object is located somewhere, right? Well, when we refer to that
location, we "point" to the object.
In common parlance, the object's location is called an "address". An object
that stores an address is called a "pointer object". A value that is an
address is called a "pointer value". We can assign pointer values to
pointer objects.
There is a special pointer value called the null pointer value which is
guaranteed not to point to any object.
To "get at" the value stored at the address pointed to by a pointer, you
"dereference" the pointer. Thus:
int i = 6; /* i is an object with type int and the value 6 */
int j = 42; /* j is an object with type int and the value 42 */
int *p = &i; /* p is an object with type pointer-to-int and the value &i */
printf("i = %d\n", *p); /* deference p; prints 6 */
p = j; /* now p points at the j object */
printf("i = %d\n", *p); /* this time, it prints 42 */
It is also possible to point at functions. When you dereference a function
pointer, you actually call the function pointed to:
double (*fp)(double) = sin;
double d = (*fp)(180.0 / 3.14); /* d gets the value 0.0 */
fp = cos;
d = (*fp)(180.0 / 3.14); /* this time, d gets the value -1.0 */
When you do arithmetic with pointers, it is done in terms of the objects to
which they point. So if, say, you have an array of ints (and, as you may be
aware, ints normally are a tad bigger than a single byte):
int myarray[6] = { 0, 1, 2, 3, 4, 5 };
int *p = myarray; /* p points to first int in myarray array */
++p; /* p now points to /second/ int in myarray array, NOT the second byte
in the first int! */
One last point: it's not generally possible to know whether a pointer value
is valid or not unless its value is NULL, in which case you know for sure
that it's invalid. This is useful information.
That's it. Pointers are really that simple. If you're still having trouble
with pointers, ask a more specific question.