*Pointers

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

*Pointers

 
0
  #1
Apr 21st, 2005
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!


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.

  1. double *p;
  2.  
  3. p = 0; // valid
  4. p = NULL; // valid
  5. 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:
  1. The following example provides the type DRAWF for a function returning no value and taking two int arguments:
  2.  
  3. typedef void DRAWF( int, int );
  4. After the above typedef statement, the declaration
  5.  
  6. DRAWF box;
  7. would be equivalent to the declaration
  8.  
  9. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: *Pointers

 
0
  #2
Apr 21st, 2005
Originally Posted by JoBe
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:
  1. Expression----------------Type--------------Value
  2. p-------------------------pointer-to-double--an adress
  3. *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

Originally Posted by JoBe
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.
  1. typedef char *pointer;
  2. char *p, *q;
  3. pointer p, q;
Because if you did the following, it would not be the same.
  1. char* p, q;
  2. 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.
Last edited by Dave Sinkula; Apr 21st, 2005 at 11:47 am. Reason: Missing a very important 'not'.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: *Pointers

 
0
  #3
Apr 21st, 2005
>>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.
  1. char* p, *q;
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: *Pointers

 
0
  #4
Apr 21st, 2005
>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.
  1. typedef char *pointer;
Now you can declare variables that have type char* like this.
  1. pointer a, b;
Both a and b would have type char*, just as if you had done this.
  1. 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.
  1. char* a;
To declare two pointers, then, s/he might try this.
  1. 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:
  1. 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
  1. **y = 0;
would you be surprised or confused?

Would it be less confusing if instead it were written like this?
  1. X *foo(Y ***y)
  2. {
  3. **y = 0;
  4. }
To each his own, but I prefer the latter.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: *Pointers

 
0
  #5
Apr 21st, 2005
Yep, that made it much clearer, thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1699 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC