+ 2
Please explain me about defrence operator and pointerss from basic to advance ..
Plz tell me I am a begginer and I am not getting pointerss... (14 years)
3 Answers
+ 5
Pointers are variables that hold other variables' memory addresses. For example:
int a = 8; //value of a is 8
int *p = &a // p now points to a
cout << *p // Output: 8
Note that the deference operator (*) is used to declare pointers are manipulate the value the pointer is pointing to. The address-of operator (&) is used to get the address of a variable.
If for whatever reason you want the address of a, you could do this:
cout << p // Output: 0x28ff18 (may change, depending on where the memory is stored)
OR you could also do this:
cout << &a //Output: 0x28ff18
You can set the value of different variables via pointers, for example:
#include <iostream>
using namespace std;
int main(){
int first, second;
int *p;
p = &first // p points to first
*p = 1 // set value pointed to by p (i.e. first's) to 1
p = &second //p now points to second
*p = 2 //set value pointed to by p (now second's) to 2
cout << first << endl; // Output: 1
cout << second << endl; //Output: 2
}
(Example shamelessly copied from cplusplus.com)
Pointers may be more efficient in certain situations. Hence, it is worth learning more about them. This is only the basics; you can Google to find out more.
How this helped.
(P.S. You may be 14 but that doesn't mean you are a beginner. There are many very good coders here who are younger than or as young as you. đ Just try hard and keep learning!)
+ 4
pointers are variables that point towards other variables!From my little knowledge the value of a pointer is the memory location of another variable I guess!