+ 2

what is the difference between using * (pointer) and (&) for finding address. ?

both give us address ??

30th Sep 2016, 9:38 AM
rahul negi
rahul negi - avatar
8 Respostas
+ 6
#include <iostream> using namespace std; int main() { int number1 = 88, number2 = 22; // Create a pointer pointing to number1 int * pNumber1 = &number1; // Explicit referencing *pNumber1 = 99; // Explicit dereferencing cout << *pNumber1 << endl; // 99 cout << &number1 << endl; // 0x22ff18 cout << pNumber1 << endl; // 0x22ff18 (content of the pointer variable - same as above) cout << &pNumber1 << endl; // 0x22ff10 (address of the pointer variable) pNumber1 = &number2; // Pointer can be reassigned to store another address // Create a reference (alias) to number1 int & refNumber1 = number1; // Implicit referencing (NOT &number1) refNumber1 = 11; // Implicit dereferencing (NOT *refNumber1) cout << refNumber1 << endl; // 11 cout << &number1 << endl; // 0x22ff18 cout << &refNumber1 << endl; // 0x22ff18 //refNumber1 = &number2; // Error! Reference cannot be re-assigned // error: invalid conversion from 'int*' to 'int' refNumber1 = number2; // refNumber1 is still an alias to number1. // Assign value of number2 (22) to refNumber1 (and number1). number2++; cout << refNumber1 << endl; // 22 cout << number1 << endl; // 22 cout << number2 << endl; // 23 }
30th Sep 2016, 11:51 AM
Alexander Johnson
Alexander Johnson - avatar
+ 6
A reference variable provides a new name to an existing variable. It is dereferenced implicitly and does not need the dereferencing operator * to retrieve the value referenced. On the other hand, a pointer variable stores an address. You can change the address value stored in a pointer. To retrieve the value pointed to by a pointer, you need to use the indirection operator *, which is known as explicit dereferencing. Reference can be treated as a const pointer. It has to be initialized during declaration, and its content cannot be changed. Reference is closely related to pointer. In many cases, it can be used as an alternative to pointer. A reference allows you to manipulate an object using pointer, but without the pointer syntax of referencing and dereferencing. The example illustrates how reference works, but does not show its typical usage, which is used as the function formal parameter for pass-by-reference.
30th Sep 2016, 11:52 AM
Alexander Johnson
Alexander Johnson - avatar
+ 4
A reference is a name constant for an address. You need to initialize the reference during declaration. int & iRef; // Error: 'iRef' declared as reference but not initialized Once a reference is established to a variable, you cannot change the reference to reference another variable. To get the value pointed to by a pointer, you need to use the dereferencing operator * (e.g., if pNumber is a int pointer, *pNumber returns the value pointed to by pNumber. It is called dereferencing or indirection). To assign an address of a variable into a pointer, you need to use the address-of operator & (e.g., pNumber = &number). On the other hand, referencing and dereferencing are done on the references implicitly. For example, if refNumber is a reference (alias) to another int variable, refNumber returns the value of the variable. No explicit dereferencing operator * should be used. Furthermore, to assign an address of a variable to a reference variable, no address-of operator & is needed.
30th Sep 2016, 11:49 AM
Alexander Johnson
Alexander Johnson - avatar
+ 3
*(pointer) give us contents of a variable. example: int x=5; int *p=&x; cout<<*p;//it will give value of x,which is 5. & give us address. example: cout<<&x;//it will give address of x.
30th Sep 2016, 11:50 AM
Atif Iqbal
Atif Iqbal - avatar
+ 1
pointer is pointing address and & is name given to a variable
30th Sep 2016, 9:42 AM
Soutik
Soutik - avatar
+ 1
* is the value of the stuff that its pointing at and & is for its address
30th Sep 2016, 7:32 PM
Ayin GhaziMoradi
0
Say you have four boxes named ( 1,2,3,4) and in each box there is a value (21,6,98,0.21). What address (&) does is it gives you the location of the box :1,2,3,or 4. What a pointer(*) does is it gives you the value that is inside that box: 21,6,98,0.21. For ex. i want to know what is stored in box 3, i would write int x=98; int *p; (which declares both) *p=&x (this allocates the pointer/address to the asterisk) cout<<*p; (this would give you an output of 98)
5th Oct 2016, 12:56 AM
jon reyn
jon reyn - avatar
0
simply put.. *<variable name> is used to declare a variable as pointer ... &<variable name> is used to store memory address of the required variable..
7th Oct 2016, 4:40 AM
ayudh nandy
ayudh nandy - avatar