0

Pointer

what is the difference between *and& opreaters in pointer how it works?

6th Dec 2016, 12:15 AM
AbuAli
AbuAli - avatar
4 Answers
+ 2
//we create a variable. int a = 5; //it has a name, and a value that is stored in memory under a specified address, and occupies amount of bytes specified by it's type //then, we create a pointer int* p; //pointers are variables, but they are made to store not the value, but only address of some variable. they are declared with the same type to know how many bytes after this address the variable occupies. //next, we take the address of variable a (with & operator) and store it in pointer (simply assign it) p = &a; //now p holds address of a, and we can access it's value without name, just by address. *p = 10; //same as a = 10; //* operator on pointer treats it like a variable stored under this address, without this sign you change and read address in memory, with it - value. behind this all, if you don't understand something in tutorial (which is made to teach you this thing), try something from this list: - read it one more time and try to understand; - take some practice, maybe this will help; - search the web, you are not the first who doesn't understand this; - take a break, get some sweets, sleep, and then repeat :)
6th Dec 2016, 12:37 AM
Demeth
Demeth - avatar
+ 2
* and & are inverses of each other. (Similar to + and -). & returns a pointer to (the address of) its operand. * returns a reference to the variable its operand points to. int i = 5; int j = 10; int *p; // Pointer to an integer. p = &i; // p now points to i. cout << *p << endl; // Displays 5. *p = 0; // The variable pointed to by p (i in this case) is set to 0. cout << i << endl; // Displays 0. p = &j; // p now points to j. cout << *p << endl; // Displays 10. *p = 20; // The variable pointed to by p (j in this case) is set to 20. cout << j << endl; // Displays 20. C and C++ have rich and expressive ways to use pointers for array manipulation, dynamic data structures and parameter passing. More than can be done justice to in a brief post. There are many books and websites that cover the topic. Google is your friend on this.
6th Dec 2016, 1:01 AM
Gordon Garmaise
Gordon Garmaise - avatar
+ 1
* is used during a variable declaration to identify that this variable is a pointer, i.e., the variable can store information which is another variable's address. In other hand, the address-of operator (&) is used to retrieve the variable's address. For instance: int* p; // pointer that points integer variable's adress. int i = 3; p = &i; // pointer p receives i's address. The * is also used to retrieve information present in a adress pointed by the pointer variable. Using the example above, we'll have: cout << *p; // terminal presents i's value (3).
6th Dec 2016, 12:40 AM
Adriano Armani
Adriano Armani - avatar
+ 1
thank you guys, I got now. #include <iostream> using namespace std; int main() { int a = 333, *p=&a; cout<<*p<< "\n"<<p<<"\n"; // the value and address return 0; }
6th Dec 2016, 2:33 AM
AbuAli
AbuAli - avatar