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 word

  • use display() display wherever pointer pointing to

    in 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

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -