Where better to read, what are pointers in C ++, and then I read and nothing is incomprehensible?

2 answers 2

For example , at address 0x00000001 there is a variable of type int with a value of 5:

//0x00000001 5 int* var = new int; *var = 5; delete var; 

so var is 0x00000001, and * var is 5

  • * this is a dereference operator (i.e. * var means to take the value to the address stored in the var variable)
  • & this is the address taking operator (not to be confused with links)

In the previous example from @Sever , it would be clearer (IMHO) to write

 int a=10; //например адрес а = 0x00000001 int с=15; //например адрес с = 0x00000002 int *b; //тут b объявленный указатель b = &a; //a == 10, &a == 0x00000001, и теперь b == 0x00000001, *b == 10 b = &с; //b == 0x00000002, *b == 15 

Links

Everything is quite simple, when declaring a variable, it is written & and then the address cannot be changed

 int a = 10; int &b = a; //a и b две одинаковые переменные (с тем же значением) //и если изменить одну, соответственно во второй будет тоже измененное значение a == 10; b == 10; a = 15; b == 15; b = 0; a == 0; 

Example with function

 void MultipliedByTwice(int* var) { *var = (*var) * 2; } int main () { int num = 32; MultipliedByTwice(&num); //тут num == 64 return 0; } 

That is, we can say that pointers are variables that store the address. If you are going to change the address stored in the function in the pointer, you need to have the same variable that you passed to the function link to it:

 void ResizeString(char*& resbuffer, long NewSize) { char* newbuffer = new char[NewSize+1]; strcpy_s(newbuffer, NewSize, resbuffer); delete[] resbuffer; resbuffer = newbuffer; } int main () { char* str = new char [4]; strcpy(str, "Hi!"); printf(str); ResizeString(str, 12); //str == "Hi!\0????????" strcat(str, " And by."); //str == "Hi! And by.\0" printf(str); delete str; return 0; } 

The point is that when you pass something to a function, the function declares its copies of the variables you passed, i.e. eg:

 int a=10; //например адрес а = 0x00000001 int *b; //адрес b например равен 0x00000002, т.е. &b == 0x00000002 b = &a; //a == 10, &a == 0x00000001, b == 0x00000001, и &b == 0x00000002 *b = 15 //*b == 15 && a == 15 && b == 0x00000001 && &b == 0x00000002 MultipliedByTwice(b); void MultipliedByTwice(int* var) { //а вот тут уже var == 0x00000001, но &var равен (например) 0x00000003 *var = (*var) * 2; } 

Pointer array

Since the pointers are variables, the arithmetic apertions are also applicable to them:

 char stackstr[] = "My first string!"; char* strptr = &stackstr[0]; //создаем указатель на первый элемент массива printf(strptr); //выведет "My first string!" strptr++; printf(strptr); //выведет "y first string!" strptr += 8; printf(strptr); //выведет "string!" strptr = strptr - 6; printf(strptr); //выведет "first string!" 

Ie for example the address of the first element of the array 0x00000001, then the second 0x00000002 respectively, the third 0x00000003, etc. The pointer is a variable containing the address itself (which can be changed) and in the latter case it is clearly seen that each character of the array has its own address, and that the pointer to the array simply stores the address that indicates the value of the char type (I’m saying that renaming the pointer "printf (* strptr);" will display the character stored at the address, rather than the entire string

Example from @avp

 void substr(const char* src, int start, int count, char* destBuffer) { //Тут наглядно видно что именно Создался указатель src из stackstr, и destBuffer из buffer if (start > 0) src += start; if (count > 0) while(count--) *destBuffer++ = *src++; *destBuffer = 0; } int main() { char stackstr[] = "My first string!"; char buffer[6]; //из stackstr начиная с 3 символа скопировать 5 символов в buffer substr(stackstr, 3, 5, buffer); printf(buffer); //выведет "first" return 0; } 

ps I don’t know whether it’s clear or not clear, but it’s not the first time either that came to me.

  • one
    @ lirik90, what is this nonsense with substr? Why move src in a loop? Why any extra counters? if (start> 0) src + = start; if (count> 0) while (count--) * destBuffer ++ = * src ++; IMHO something like that. - avp
  • Yes, something I really ... do not sleep;) Thank you! - lirik90
  • And I remembered, I had just a check for the end of str there: for (int i = 0; i <start; i ++, pos ++) if (* src ++ == 0) return; But in any case it is better this way: while (start--) if (* src ++ == 0) return; - lirik90

All variables are stored in memory. A pointer is a memory address that points to (or refers to) a specific area.

For example:

 int a=10; int *b=&a; //в "b" записали адрес переменной "a" 
  • > All variables are stored in memory. Not all. Counter variable, for example, as a result of optimization can become a register one. - insolor
  • We are talking about the general case. Here is more. ru.wikipedia.org/wiki/Addressing_of Memory - Sever
  • @Sever, I say that with register addressing you cannot define a pointer to an operand, since the register has no address. ru.wikipedia.org/wiki/Addressing_of memory #Register_addressing - insolor
  • 2
    @insolor, write register int i, * p = & i; can. gcc will issue a warning, but will .exe. Only now i will put it on the stack and before the actions with the pointer it will register in memory. - avp pm
  • one
    I repent, sometimes I am too picky :) @avp, in this case the variable just ceases to be registered. @sever,> We are talking about the general case. In general, a variable can be in memory or in a register ("variables are stored in memory" is just a special case). That's all I wanted to say. Although registers of course also memory, not only addressable, but called. - insolor