943,940 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1813
  • C++ RSS
Apr 21st, 2005
0

*Pointers

Expand Post »
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!


Quote ...
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.

C++ Syntax (Toggle Plain Text)
  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?

Quote ...
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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Apr 21st, 2005
0

Re: *Pointers

Quote 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:
C++ Syntax (Toggle Plain Text)
  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

Quote originally posted by JoBe ...
2) Can someone explain what the use is of typedef and how I can interpret this explanation?

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  1. typedef char *pointer;
  2. char *p, *q;
  3. pointer p, q;
Because if you did the following, it would not be the same.
C++ Syntax (Toggle Plain Text)
  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'.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 21st, 2005
0

Re: *Pointers

>>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.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Apr 21st, 2005
0

Re: *Pointers

>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.
C++ Syntax (Toggle Plain Text)
  1. typedef char *pointer;
Now you can declare variables that have type char* like this.
C++ Syntax (Toggle Plain Text)
  1. pointer a, b;
Both a and b would have type char*, just as if you had done this.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. char* a;
To declare two pointers, then, s/he might try this.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. **y = 0;
would you be surprised or confused?

Would it be less confusing if instead it were written like this?
C++ Syntax (Toggle Plain Text)
  1. X *foo(Y ***y)
  2. {
  3. **y = 0;
  4. }
To each his own, but I prefer the latter.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 21st, 2005
0

Re: *Pointers

Yep, that made it much clearer, thanks
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: struct array and enum problems
Next Thread in C++ Forum Timeline: run code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC