pointers - Passing address of a char* C++ -
void display(char* word) { static char* pointertoword = word; cout << pointertoword; } void initialise(char* word) { display(word); } void main() { char* word[3]; char* currentword; word[0] = "hello"; word[1] = "world"; word[2] = "hahahaha"; currentword = word[0]; initialise(currentword); currentword = word[1]; //displays word[0] display(0); currentword = word[2]; //still displays word[0] display(0); }
char* bit of pain in neck. can me syntax right?
all want is
initialise()
display()
's pointer current worduse
display()
display wherever pointer pointing toin reality i've got few classes involved, example pretty illustrates problem. have no intention of modifying string, strings constant.
change code follows: first put pointertoword
@ global scope:
static char* pointertoword = "";
overload display function:
void display() { cout << pointertoword; } void display(char* word) { pointertoword = word; display(); }
Comments
Post a Comment