I read several articles on the Internet about pointers, but I didn’t figure it out.

Here is the code:

//--------------------------------------------------------------------------- #pragma hdrstop //--------------------------------------------------------------------------- #include <stdio.h> #include <iostream.h> #pragma argsused using namespace std; int main(int argc, char* argv[]) { int *pointer; *pointer = 1000; cout << pointer << endl; cout << *pointer << endl; cout << &pointer << endl; getchar(); return 0; } //--------------------------------------------------------------------------- 

Actually, what is the question: what is the difference between & and the dereferenced variable in the output (by the way, when compiling everything is different in the output)?

  • > by the way, when compiling everything is different when outputting, that's what makes the difference. Sincerely your cap - DreamChild
  • So why outputs different, if & is used to output a variable address? - MaximPro
  • @MaximPro damn, well, at least you’ll see what exactly cout << * pointer << endl displays and what cout << & pointer << endl displays. It’s easy to come up with some simple logical conclusions that once the output matches the content in the first case , which you previously assigned to the variable * pointer, then in the first case the contents of the variable are output, and in the second - the address - DreamChild
  • 1db4874 <br> 1000 <br> 18ff50 <br> - MaximPro
  • It is clear that co * is dereferencing a variable, and we will get the value in response. More concerned about the issue is just a conclusion without a star, then what is it? Address? If so, why is it different from output with &? - MaximPro

3 answers 3

You create a pointer to a pointer and you are surprised that it does not match the data ...%)

I advise you to write more C programs and use pointers more often. After a while it will become natural to breathe. Perhaps this “picture” will clarify everything:

 #include <stdio.h> int main(void) { int data = 1000; int* pointer = &data; int** pointer_to_pointer = &pointer; printf("%d %d %d\n", data, *pointer, **pointer_to_pointer); printf("%p %p\n", (void*)pointer, (void*)*pointer_to_pointer); printf("%p\n", (void*)pointer_to_pointer); return 0; } 

For practical tasks, to get your hand, you can use the site CodeAbbey.com

  • where I created a pointer to a pointer and wonder? - MaximPro
  • For: int * p; the result of the operation: & p will be the so-called pointer to the pointer. Those. To save its value, you need the following variable: int ** pp; - avp
  • That is, if I make a link to the pointer through what I get? If I understand correctly, then simply the output of pointer is its address (the address of the pointer in the memory). But with & then there are questions ... I just just started reading the topic pointers and so far I have one feeling - confusion How does this mean that this is a pointer to a pointer? If write clearly (do as an answer), I will choose as the answer to my question, because what you have written more adequately sounds - MaximPro
  • 2
    @MaximPro, in the comments to his answer, @elCorsaiR correctly wrote about memory and addresses. In fact, nothing to add. Apparently the whole thing is in terminology and synonyms (the pointer and the address are synonyms in a certain sense, the essential difference is that the pointer is typed . If we add to the value of pointer 1, the address it points to (i.e. the pointer value as numbers) will increase not by 1, but by the size of the type of variable that the pointer is addressing). It is better, of course, to read about it in more detail, for example, in the well-known book - K & R. - avp 2:32 pm
  • one
    @MaximPro: I painted everything, and it was through &. Could notice that pointer turned out exactly as & data. That is, & is the address. A pointer is a variable in which the address of something is written. Maybe you really draw? There is a box in which lies 1000 rubles (and better dollars). The box is called data. The & operator gives the coordinates of the data box (for example, the tenth from the wall). But this number is also data. They also need a box (variable). Let's call this box pointer. Then & pointer is its ordinal number. But * pointer is data from a box whose address is in a pointer box - VadimTukaev

cout << pointer << endl;

  • the address where your value is stored

cout << & pointer << endl;

  • the address where the pointer is stored (such as the address pointing to the place where the address is stored where your value is stored).
  • the meaning of what? numbers? 1000? - MaximPro
  • yeah, 1000. - elCorsaiR
  • if I understand correctly, the output is as follows: 1) We have a pointer that already has a value of 1000, let's say. 2) Address your pointer. 3) The address to the pointer value is also yours. So what? Above write that & pointer is a pointer to a pointer. I'm completely confused. - MaximPro
  • Not really. Let's start with the fact that we have a memory, each memory cell has an address, and a value can be stored in this cell. All variables are located at some address and pointers too. From your example: 1db4874 is the address of the memory cell where the value 1000 is stored. if you go to the address 1db4874 in the memory browser, you will see the value 1000 there. 18ff50 is the address where the pointer is stored. those. If in the memory browser you go to the address 18ff50, you will see the value 1db4874 there. - elCorsaiR
  • How can I see it? - MaximPro

For better understanding it is better to write like this:

 int* pointer; 

where not the type is something name , but the type name , that is, int * is better to be considered a separate "type". And if you look at the type of our variable, you can understand that

 cout << pointer; 

returns exactly the value of the "type" int , where & pointer is the address where our value is stored *.

Where value is the address to the section of memory that will be allocated for the type int during program execution, which can be obtained as follows:

  cout << *pointer; 
  • * pointer - I understood a long time ago how I read an article on the Internet, I’m interested in the difference in the output of pointer and & pointer - MaximPro
  • I have already written about this (under the second block with the code) - Ni55aN