I am learning C++, but I don't completely understand the mechanic of using pointers.
How can I achieve filling array tab2 using pointers in this code:
int * tab1 = new int[10];
int * tab2 = new int[10];
for (int i = 0; i < 10; i++){
tab1[i] = i;
*tab2 = i;
tab2++;
}
for (int i = 0; i < 10; i++){
std::cout << tab1[i] << "\t" << tab2[i] << std::endl;
}
The teacher in my school doesn't explain it clearly and I don't understand how to adjust array elements using pointers and putting new values in it. Please, help me understand a correctly working example with it.
*tab2 = i; tab2++;it is simple. At the start of the loop the pointer is at the beginning oftab2pointing to its 1st element. Dereferencing it*tab2alows to assign value directly to element pointed by pointer.tab2++moves pointer to the next element, then rinse and repeat.newthis is mainlyC-Code and Style. That means also mainly badC++. In C++ you would usestd::vectororstd::array