I'm learning C++ programming, but I can't really understand what pointers are and how do they work. Can anyone explain it please?
Thank you very much.
pointers work with the directions in memory of your variables. If you start playin' with'em you'll find it's so much easier than many of us though it could be. believe me, you have to try and try your codes. anyway, you can always consult a lot of c++ tutorials in tha web( plenty of them) just f* google it man!...
it's hard to explain in a short sentence...
but i'll try:
a pointer as it's name, points to a sertain place in the memory, think of a box, which can have in it only integer number, that box name is nBox.
in c++ you will define that box as: int nBox.
but that box also has an adress, which is easier to work with its address, because all boxes, and other object have an adress in the same format.
that adress is a pointer...
my english is not the best, hope you'll understand something from it, if you need more information, you can PM me, or search google...
the big disadvange with pointers is that you won`t understand them well untill much later when you work with classes, polymorphidm, etc... the basic ideea is this:
you have a variable x = 1; the variable x is stored on your computer at a specific memory adress. With pointers you acces the information stored in the variable. Instead of having another variable to hold the same information (a copy) you have a "pointer" that points to that information. Like someone just said, play around with them and as you continue to learn c++ you will get the hang of them.
Pointers: are simply like they sound a reference to an area of memory.
They can be extremely useful in C++ coding as they allow dynamic allocation of memory etc.
However, as a suggestion if you are in the early stages of learning C++ programming, and don't mind too much using microsoft tools then I suggest you start with C# (only if your mainly interested in windows programming).
The advantage of this is that although you will be using pointers to some extent - you won't run into nearly as many memory leak problems as it is managed coding.
See the microsoft sight for Visual C# express edition - can download for free a pretty good IDE.
Nice to know you are learing C++. A pointer is a special but a normal type of variable. Its normal in the sense, it has a type, for example int, char, float, double or void. Its special in the sense, it cannot contain a value instead it can only contain an address of another variable. Like others have said, the best way to learn and understand pointers is by experimenting with them. Here is the help I can offer you:
How to declare a pointer to an integer variable?
Notice, to declare a variable as a pointer, the name of the variable has to be preceeded with an asterik, i.e. '*'
How to assign an address of a variable to a pointer?
Assuming we have an integer variable declared as:
To assign ptrToIngeter the address of the variable intVariable, this is what you have to do:
ptrToInteger = &intVariable;
This creates an indirect reference to the intVariable. To referer to the value of variable intVariable using the ptrToIngeteger pointer, you'll have to do what is known as, dereference the pointer variable. To dereference, you'll have to preceed the pointer variable with an asterik, just like you do when you declare it.
An example which illustrates all the concepts and blah is as follows:
// This declares a pointer named ptrToInteger of type int. We
// can use this pointer to point to any
// integer variable.
int intVariable = 10;
// A normal variable which is assigned a value of 10
ptrToInteger = &intVariable;
// When the & operator precedes a variable name, it gives out
// its address in memory. So here,
// &intVariable assigns the address of variable intVariable into
// the ptrToInteger pointer
*ptrToInteger = 500;
// Even though the value 500 is beign assigned to
// ptrToInteger, its actually beign assigned to
// variable intVariable because ptrToInteger is just a pointer
// to it
cout << intVariable << "\n";
// Notice the output of intVariable. It will be 500. The
// ptrToInteger pointer changed its value.
If you are learning c++ pointers, it will be really har to understand it here. You will be much better of reading iot from a book. Bruce Eckel's thinking in c++ is my personal favorite book, and its also available for free. Google Bruce Eckel.[/url]
|ben8coast wrote: |
|I suggest you start with C# (only if your mainly interested in windows programming). |
Not meaning to drag this further off topic but don't forget that mono exists as an open source cross platform .net compatiable framework. (http://www.mono-project.com/Main_Page)
Also pointers are hard but make a lot of sense as they stop stuff caching stuff. Look for diagrams as I found them the most helpful in understanding them.
pionters are used to call variables by reference . like asking some one to handle you the cup in the cup-holder rather than the cup itself . complicated ,isn't it ?