c++ - how do i access an array returned as a pointer from a function using a subscript? -


i have created function returns pointer array of strings. function should traverse linked list , should assign data each node array of string. here function:

//function traverse every node in list string *dynstrstk::nodestrings(int count) {     stacknode *nodeptr = nullptr;     stacknode *nextnode = nullptr;     int = 0;      //position nodeptr @ top of stack     nodeptr = top;      string *arr = new string[count];      //traverse list , delete each node     while(nodeptr != nullptr && < count)     {         nextnode = nodeptr->next;         arr[i] = nodeptr->newstring;         nodeptr = nextnode;          cout << "test1: " << arr[i] << endl;     }      return arr; } 

i want use pointer array returned function above, , want assign new array in different function test each subscript in array condition.

i having trouble accessing new array , can't print out string in each new array element.

arr = stringstk.nodestrings(count); cout << "pointer arr of str: " << *arr << endl; for(int = 0; < count; i++) {     cout << "test2: " << arr[i] << endl; } 

this output after calling both functions:

test1: rotor test1: rotator test1: racecar test1: racecar pointer arr of str: racecar //this test tells me can array test2: racecar test2:  test2:  test2: 

this expected output

test1: rotor test1: rotator test1: racecar test1: racecar pointer arr of str: racecar test2: racecar test2: racecar test2: rotator test2: rotor 

what doing wrong , how can access each element in new array second function??????

thanks!!!!

here second function using pointer array:

int createstack(fstream &normfile, ostream &outfile) {     string catchnewstring;     string teststring, revstring;      string *arr;      int count = 0; //counts number of items in stack      dynstrstk stringstk;      while(getline(normfile,catchnewstring)) // read , push stack     {         stringstk.push(catchnewstring); // push stack         //tracer rounds         outfile << catchnewstring << endl;         count++;      }       arr = stringstk.nodestrings(count);      cout << "pointer arr of str: " << *arr << endl;      for(int = 0; < count; i++)     {          cout << "test2: " << (arr[i]) << endl;     }      return count; } 

you forgot increment i in function dynstrstk::nodestrings. therefore assignments arr[0].


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -