c++ - Passing pointer by reference—cannot change value -


i've read many topics passing pointers function reference, couldn't find answer. problem after pass pointer reference , change value, after leaving function, value of original pointer doesn't change. stuck it. please me! you're hope!

btw. code pasted same code need work on, , cannot change calling of function. code:

void f(char *p){     char *np = new char(100);     np = "aaaaaaaaaaaaaaaaa";     p = np; } 

and calling of function:

void *ptr; f((char *)&ptr); 

i thankful help!

your question unclear, need guess @ question. guess of question cannot change signature nor call of function

void f(char* p); ... void *ptr; f((char *)&ptr); 

and want change body of f, change ptr point literal within body of f. if guessed question right, answer be:

void f(char* p) {    *((char**)(p)) = "aaaaaaaaaaaaaaaaa"; } 

if didn't guess question correctly, please ask more clearly.

in call have cast void** char*, dropping level of indirection, inside function cast level of indirection in, , @ same time change ultimate target void char. isn't bizarre seems (or have guessed differently @ question). casting things to/from char* may aliasing issues (when otherwise compiler optimize whole operation out of existence). maybe question part of legitimate. ugly enough there must better way.


Comments

Popular posts from this blog

javascript - How to process users in one specific order using map o each function? -

javascript - Linking from page A to a specific iframe on page B -

java - Two interface have same method name with different return type -