c++ - Why can't I change address of a pointer of type `const int *` when passed as function argument? -


as far know, const int * implies can change pointer not data, int * const says can't change pointer address can change data, , const int * const states can't change of them.

however, can't change address of pointer defined type const int *. here example code:

void func(const int * pint) {     static int int = 0;     pint = &int;     int++; }  int wmain(int argc, wchar_t *argv[]) {     int dummy = 0;     const int * pint = &dummy;     //const int * pint = nullptr;   // gives error when try pass func().     std::cout << pint << '\t' << *pint << std::endl;     std::cout << "-------------------" << std::endl;     (int i=0; i<5; i++)     {         func(pint);     // set pointer internal variable. (but, doesn't set it!)         std::cout << pint << '\t' << *pint << std::endl;     }      return 0; } 

code output:

00d2f9c4        0 ------------------- 00d2f9c4        0 00d2f9c4        0 00d2f9c4        0 00d2f9c4        0 00d2f9c4        0 

i expect address of pint change point internal variable inside func() function after calling func() @ least once. doesn't. keeps pointing @ dummy variable.

what happening here? why don't result expect?

(ide: visual studio 2015 community version)

you don't see change @ call site because passing pointer value. modifying inside func change local copy, not pointer passed in.

if want modify pointer , have changes visible outside, pass reference:

void func(const int *& pint) //                   ^ 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -