D
D
Denis Triel2019-04-10 13:54:34
C++ / C#
Denis Triel, 2019-04-10 13:54:34

Question about pointers in C++. A variable assigned to a pointer will always go "&Variable" in the code??

Description of the issue:
When declaring a pointer and a variable

int* pPointer = 0;
int x = 1;
pPointer  = &x;

Then it is mandatory to always assign the symbol & to the variable if I call it, for example: The
cout << &x ;
question may be stupid, but I asked it for the sake of interest.
Thank you for your attention.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
GavriKos, 2019-04-10
@Trielsiren

The question is really stupid. Read what the & does in front of the variable name - and you will find the answer to your question there.
And if you answer directly - no, not always.

T
tsarevfs, 2019-04-10
@tsarevfs

The & in front of the variable is the address.
The analogy with the address as with the coordinates of a physical object works quite well. You can write down the coordinates on a piece of paper and get a "pointer". If you write down the coordinates of the place where you store the first sheet, then you will get a pointer to a pointer. To simply copy a pointer, you do not need to take its address.

int apple = 1;
int* poinerToApple = &apple;
//просто скопируем указатель
int* poinerToApple2 = poinerToApple;
//ожидаемо оба указывают на одно значение
//assert покажет что условие верно
assert(*PointerToApple == *PointerToApple2); 
assert(*PointerToApple == apple);
assert(*PointerToApple2 == apple);


//так получим указатель на указатель
int** poinerToPointerToApple = &poinerToApple;
int** poinerToPointerToApple2 = &poinerToApple2;

//разные указатели имеют разные адреса
assert(poinerToPointerToApple2 != poinerToPointerToApple);
//но они могут указывать на один и тот же указатель
assert(*poinerToPointerToApple2 == *poinerToPointerToApple);
//и по ним мы можем добраться до исходного значения
assert(**poinerToPointerToApple2 == apple);

In general, you cannot call a variable. You can call the function f() and some special objects. You described the output to the console.
int x = 5;
cout << x; //напечатает 5
cout << &x; //напечатает что-то типа 0x7fffffffd2e4

M
Mr Crabbz, 2017-02-09
@Punkie

How are you going to work with Paypal in Ukraine? We don't officially have it and it's not expected in the near future.

I
Igor Mavlikhanov, 2017-02-09
@Gori4ka

woocommerce has paypal by default. You need to enter your PayPal email, and when paying with PayPal, it will convert from uah to your currency, which is on PayPal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question