How to return char* array in c/c++ function? -


following function prototype(we can't change prototype of function):

char** myfun() {   // implementation } 

how can return array of char* function can access/print content of array in caller of method. tried creating array using dynamic memory allocation,but not working.

is possible without dynamic memory allocation. please provide me pointer?

unless have global char*[] want return function, yes, need dynamic allocation, way can safely return variable created inside function, variables stored in stack destroyed function finishes.

char** myfun() {      char **p = new char*[10];      return p;      //or return new char*[10]; }  int main() {      char **pp = test();      char *p = "aaa";      pp[0] = p;      delete[] pp; //if allocate memory inside array, have explicitly free it:                  //example: pp[1] = new char("aaa"); require delete p[1];   } 

--edit

you don't have use dynamic allocation, return local static variable. keep in mind not approach though: function not thread safe or reentrant.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -