Moving an array pointer does not change. ADDRESS start

My code is:

#include <iostream>

using namespace std;

int main() {
    char *test = (char*)malloc(sizeof(char)*9);
    test = "testArry";
    cout << &test << " | " << test << endl;
    test++;
    cout << &test << " | " << test << endl;
    return 1;
}

Result:

004FF804 | testArry
004FF804 | estArry

I do not understand how it is possible that I moved the pointer and the address of the array has not changed.

+4
source share
2 answers

The pointer has changed. You just don't print it. To print a pointer test:

cout << (void*) test << endl;

&testis the memory cell in which it is stored test.
testis the value that you increased with test++(i.e., you did not increase &test).

cout << test operator<<, , - , const char* C, , . void* , test , .

+9

cout << &test << " | " << test << endl;

&test test, , , . , .

test, , ,

cout << ( void * )test << " | " << test << endl;

, , . .

const char *test;
+1

Source: https://habr.com/ru/post/1671514/


All Articles