| | |
*Pointers
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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!
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?
Ive found this on the net:
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
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.
C++ Syntax (Toggle Plain Text)
double *p; p = 0; // valid p = NULL; // valid p = 123; // not valid
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;
C++ Syntax (Toggle Plain Text)
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 );
Don't understand however what's happening with the example with pointers?
Any additional info is greatly appreciated as usual
•
•
•
•
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:
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?C++ Syntax (Toggle Plain Text)
Expression----------------Type--------------Value p-------------------------pointer-to-double--an adress *p------------------------double-------------(12.34)
Also, when do you use this?
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;
C++ Syntax (Toggle Plain Text)
typedef char *pointer; char *p, *q; pointer p, q;
C++ Syntax (Toggle Plain Text)
char* p, q; pointer p, q;
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
>>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.
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
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)
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
>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.
Now you can declare variables that have type char* like this.
Both a and b would have type char*, just as if you had done this.
Some folks prefer to write the * closer to the type (not me, though). So normally a single pointer s/he would declare like this.
To declare two pointers, then, s/he might try this.
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:
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 would you be surprised or confused?
Would it be less confusing if instead it were written like this?
To each his own, but I prefer the latter.
Between my cold and my edits, let me try this again.
Let's say you have this.
C++ Syntax (Toggle Plain Text)
typedef char *pointer;
C++ Syntax (Toggle Plain Text)
pointer a, b;
C++ Syntax (Toggle Plain Text)
char *a, *b;
C++ Syntax (Toggle Plain Text)
char* a;
C++ Syntax (Toggle Plain Text)
char* a, b;
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)
pX foo(pppY y);
C++ Syntax (Toggle Plain Text)
**y = 0;
Would it be less confusing if instead it were written like this?
C++ Syntax (Toggle Plain Text)
X *foo(Y ***y) { **y = 0; }
"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
![]() |
Similar Threads
- Pointers (archived tutorial) (C++)
- Problem About Pointers (C++)
- New to C++ Pointers and need help with identifying where the errors are (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why can't I use Pointers to point to a Enumurated Constant (C++)
Other Threads in the C++ Forum
- Previous Thread: struct array and enum problems
- Next Thread: run code
Views: 1699 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






