I'm a complete newbie to C++, though I do know JavaScript, but I am clueless about understanding pointers. need some help please.
Printable View
I'm a complete newbie to C++, though I do know JavaScript, but I am clueless about understanding pointers. need some help please.
A pointer is a variable that contains a memory address. I'm assuming 32-bit now, because that's what you're most likely to be using (and 16-bit pointer methods really hurt).
When you use the & operator on a variable, you get its memory location:Notice at the moment, ptr doesn't refer to a valid location, and will contain a totally arbitrary number from somewhere in your memory. So, you have to initialise it:Code:int iNum; /* An integer variable */
int *ptr; /* A pointer to an integer variable */
Now, ptr contains the memory location of iNum, rather than iNum's value. You can get iNum's value out of this by using the * operator (indirection/dereferencing):Code:ptr = &iNum;
Code:int x = *ptr;
What parksie wrote is just the basics of pointers. Pointers are a much more complex topic. There are pointers to functions, structs etc. Sometimes they are not easy to understand. I suggest you to find a good tutorial on pointers, read it and practice of course