+ 6

Can you tell me the difference of *pointer and &pointer?

How can I manage to understand it and use on the codes? I am just new to C++ i wanted to learn this language tho, but everytime i wanted to continue my learning i ended up confused about the pointers and how they work, and how can i manage to use them?

28th Dec 2016, 3:17 AM
Mark L. M.
Mark L. M. - avatar
11 Respostas
+ 15
Now let's say we have: int* p; int q = 10; p is a pointer due to the declaration with a '*' (point-to) symbol. A pointer is used to point to the address of a variable. We store the address into the pointer by using the following syntax: p = &q; Now, we can see that I've added a '&' in front of variable q before assigning the variable to pointer p. '&' is the 'address-of' operator, which means that '&q' returns the address of variable q. Hence, p = &q; stores the address of variable q into p. We can now utilise the pointer, e.g. cout << *p; // displays 10 You can go on and do experiments with pointer values, e.g. cout << p; cout << &q; cout << *p; cout << q; cout << *&q; This will help you to understand the concept behind pointers.
28th Dec 2016, 5:24 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Simply, * is to learn for content & is to learn for address example: int x = 9; //decleration int *p = &x; //decleration cout << x; // 9 cout << *p; // 9 cout << &x; // address of x cout << p; // address of x cout << &p; // address of p Clear?
29th Dec 2016, 7:56 AM
YSF
YSF - avatar
+ 3
you can simply remember if P is pointer and A is variable which declared inside main function then int A; int *P; P=&A; here P=&A equivalent *P=A hence &(Address of Operator) give address... and if you use * before pointer (*P) it act as Variable...
29th Dec 2016, 6:22 AM
Saurabh Zinjad
Saurabh Zinjad - avatar
+ 3
it is very simple *mean the value of variable and &mean address of that variable
29th Dec 2016, 7:28 AM
Bhuwan Sharma
Bhuwan Sharma - avatar
+ 2
* is used to declare a pointer variable. eg:- int *pnum; while & is used to assign a memory address that the pointer has to hold. eg:- int a; int *pnum; pnum=&a; Now here in this case pnum pointer is going to have the memory address of 'a' as the data value stored in it.
28th Dec 2016, 6:30 PM
Pushpendra Singh Rathore
Pushpendra Singh Rathore - avatar
+ 2
hi, the difference between * pointer and & pointer is simple. Let us take a variable p. Now let's initialise the pointer int *p; let's take another variable x. Assume the value of x as 10. Now, assigning the address of x to pointer p (p = &x). Therefore cout<<p , this gives the address of x. But cout<<*p gives the value stored in x (that is 10). Quite simple.
29th Dec 2016, 6:20 AM
Suresh Karthik
Suresh Karthik - avatar
+ 2
Read * as 'value at' Read & as 'address of'
29th Dec 2016, 8:23 AM
Lokesh Agrawal
Lokesh Agrawal - avatar
+ 1
1. Any variable that has '*' before it's declaration is capable of holding address of (pointing) any variable. 2. '&' is address symbol that denotes we are dealing with address of any variable int *a; int b; a = &b; int **C; //it can hold address of variable with single '*' C = &a; //n so on
28th Dec 2016, 9:59 AM
Ayush Walekar
Ayush Walekar - avatar
+ 1
*pointer gives you the value stored in that memory location. The & operator returns the actual memory address which, in this case, is a pointer
28th Dec 2016, 1:42 PM
Manish Garg
Manish Garg - avatar
+ 1
*pointer points to the value of variable and & pointer refers to the address of the variable
28th Dec 2016, 1:58 PM
ISHITA DOGRA
ISHITA DOGRA - avatar
+ 1
So why even use pointers...isn't int p; int q = 10; p = q; able to complete the same task and simpler?? Tells what value a variable currently holds or transfer values between two variables??
28th Dec 2016, 9:19 PM
Marcus Harry
Marcus Harry - avatar