Quick Pointer Question

Started by
7 comments, last by subnet_rx 22 years, 4 months ago
I''ve set up a pointer to x as xPtr. What''s the difference between *xPtr and xPtr? subnet_rx
"There are two types of languages, those that everyone complains about, and those that no one uses."Standardized C++ LibrariesGamecron PC GamingA Christian Blog
Advertisement
The * is dereferencing the pointer. What pointers are is just variables that store an address. So for example:

xPtr = &x

That gets the address of x and stores it in xPtr. If you were to do:

cout << xPtr;

You''d get some address like 0x0000001 or something like that. If you type *xPtr, instead of getting the address, you get the value of whatever variable is held at the address it holds. So in this case *xPtr would just give you the value of x. Just a note, this is a pretty basic question and I''d suggest you look in some books before posting something like this.

--Vic--
Take a look at this:
int *x; 

x''s type is (int *) (a pointer to an integer) while *x''s type is int (an integer). I''ve found that explanation helps a lot of people.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
I''m reading the chapter now. I just didn''t understand that from the book. I just don''t know why you would need to know an address instead of a value.

subnet_rx
"There are two types of languages, those that everyone complains about, and those that no one uses."Standardized C++ LibrariesGamecron PC GamingA Christian Blog
ah, well, that''s an entirely different question.

Pointers are useful because they allow you to build arbitarily complex structures -- things like linked lists, binary trees, B-trees, graphs, red-black trees, BSP partition trees, etc. etc.

Admittedly, that can be hidden from you, like in Java, but the ability to work with pointers is a good skill to know.

Just as an excercise, try to think up a way to access an arbitrary number of integers -- I type in a number, let''s call it x, and then I give you x numbers. How does your program keep track of all of those numbers? There is no limit on what I can assign x to, although your code only has to work up to the memory limit of whatever computer it is running on. (but it should be able to run on any given computer that is running whatever OS it is your coding on). You will find it very difficult, if not impossible, to complete this without using pointers.

-Wyriel
quote:Original post by subnet_rx
I just don''t know why you would need to know an address instead of a value.

If you wanted a function to modify a value, you need to know the address of the variable. C/C++ pass parameters to functions by value (they make copies of the variable) except for arrays which are always passed by reference (address).

Don''t worry, it''ll come. Right now you may not understand the "whys" of many things, but just keep at it; you''ll figure it out when you start to apply the principles.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
thanks for the replies, they have been very helpful. I have one last question. I come from a background in Java. What''s the difference in using a pointer and just using a global variable? In Java, I just made a variable that had a class scope. In C++, it''s advising me to pass pointers to variables to change their values in functions.

subnet_rx
"There are two types of languages, those that everyone complains about, and those that no one uses."Standardized C++ LibrariesGamecron PC GamingA Christian Blog
quote:Original post by subnet_rx
What''s the difference in using a pointer and just using a global variable?

First off, pointers and global variables have no relation - they''re not even comparable paradigms. Even if you had a global variable in C/C++ which you wanted to modify through a function, you''d still have to pass either a pointer to that (global) variable or a reference (they''re similar, but not quite identical).

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
using global variables when passing a pointer is appropriate is pointers is a bad practice. There are so many things wrong with it, but basically if it seems acceptable to you then you need to up your standards in terms of what is acceptable. I am not saying that you shouldn''t use global variables, because some variables represent things that are global.

When making a variable try to match the feel with the implementation. If you have a function that sets a bunch of global variables, and then the caller of the function gets the values from there, is that what you want? When you want to hand something to your friend do you throw it on the ground and expect him to pick it up? Or do you look towards him (point) and hand it directly to him? Pointers holding addresses is not big deal, that isn''t the point. What most people don''t mention is that pointers create relationships. If you have a class with a couple variables then those variables are part of the object. Pointers let you express many more kinds. For example with a pointer to an object, you can be thinking about that object. Or you could be targeting it, or whatever, the possibilities are endless.

This topic is closed to new replies.

Advertisement