Hello ladies and gents,

Ive been reading about pointers abit and at there are two pieces of text in the book about pointers wich aren't clear to me, was wondering if any of you could enlighten me abit about them! :D

Sometimes it's desired to clearly point out that a certain pointer doesn't point to anything. We can give that pointer a value of 0. To declare this value, we have to write it either with 0 or as most of the times NULL. THis is however not a determinated word, but a constant wich in many headers, like iostream is defined like that. Another value for a pointer then 0 is not possible.

double *p;

p = 0;      // valid
p = NULL; // valid
p = 123;   // not valid

1) QUESTION IS: how can a pointer be NULL or 0 when it's an adress wich gives a value?
Because, as I understand it to be like this:
Expression----------------Type--------------Value
p-------------------------pointer-to-double--an adress
*p------------------------double-------------(12.34)

Or, is it saying that when declaring p = NULL;, the adress of that pointer is 0x00000000 and there is no value stacked in this pointer?

Also, when do you use this?

2) Can someone explain what the use is of typedef and how I can interpret this explanation?

When this is declared: typedef char *pointer

the pointer becomes an indication of the type char*. This means that the following two declarations are equivalent:
char *p;
pointer p;

Ive found this on the net:

The following example provides the type DRAWF for a function returning no value and taking two int arguments:

typedef void DRAWF( int, int );
After the above typedef statement, the declaration

DRAWF box; 
would be equivalent to the declaration 

void box( int, int );

Wich I understand, it's saying that the parameters from the function DRAWF are also the parameters for box. That I understand!

Don't understand however what's happening with the example with pointers?

Any additional info is greatly appreciated as usual ;)

1) QUESTION IS: how can a pointer be NULL or 0 when it's an adress wich gives a value?
Because, as I understand it to be like this:

Expression----------------Type--------------Value
p-------------------------pointer-to-double--an adress
*p------------------------double-------------(12.34)

Or, is it saying that when declaring p = NULL;, the adress of that pointer is 0x00000000 and there is no value stacked in this pointer?

Also, when do you use this?

A zero in pointer context or the macro NULL will both be compiled as the value of a null pointer constant. (You don't set a pointer variable's address.)

When to use it? Some like to set a pointer to NULL after freeing dynamically allocated memory so that the pointer could be checked for NULL to see if previously allocated memory has been freed.

[edit]http://www.eskimo.com/~scs/C-faq/s5.html

2) Can someone explain what the use is of typedef and how I can interpret this explanation?

When this is declared: typedef char *pointer

the pointer becomes an indication of the type char*. This means that the following two declarations are equivalent:
char *p;
pointer p;

I find this to be the worst use of typedef -- hiding a pointer declaration. It is intended to be like this.

typedef char *pointer;
char *p, *q;
pointer p, q;

Because if you did the following, it would not be the same.

char* p, q;
pointer p, q;

Even though it might appear to be. But I find typedefing pointers to be obfuscatory.

I'm sure Narue will have a better slant on this as well.

>>A zero in pointer context or the macro NULL will both be compiled as the value of a null pointer constant. (You don't set a pointer variable's address.)
Sorry Dave, could you explain this abit more, or maybe in another way? Don't exactly understand what your trying to explain?

Thanks for the additional link, I'll read that first before moving on.

>>Because if you did the following, it would not be the same.

char* p, *q;
pointer p, q;

So, in other words, what was explained in my book with the example It gave, isn't correct?

>> ...obfuscatory.
Wow, had to look that up in a dictionary, understand what it means now ;) Thanks for the English lesson Dave. I'm not only learning C++, but even improving my English here :D

>So, in other words, what was explained in my book with the example It gave, isn't correct?

Between my cold and my edits, let me try this again.

Let's say you have this.

typedef char *pointer;

Now you can declare variables that have type char* like this.

pointer a, b;

Both a and b would have type char*, just as if you had done this.

char *a, *b;

Some folks prefer to write the * closer to the type (not me, though). So normally a single pointer s/he would declare like this.

char* a;

To declare two pointers, then, s/he might try this.

char* a, b;

But here a would be a char*, but b would be a char.

But what is gained by typedefing away the pointer-ness (my term)? Let's say you are maintaining someone else's code and you run into this:

pX foo(pppY y);

Do you think you'd have a good idea of what type of variable to pass to the function? If you saw code in that function that did this

**y = 0;

would you be surprised or confused?

Would it be less confusing if instead it were written like this?

X *foo(Y ***y)
{
   **y = 0;
}

To each his own, but I prefer the latter.

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