c++ - Failed to retreive static char * array -
c++ - Failed to retreive static char * array -
i have next code snippet , intention item list , print. compiling fine @ run time , outputs not expected. have added comments result unexpected. please allow me know doing wrong here.
#include <iostream> using namespace std; class csample { private: static const char *list1[]; static const char *list2[]; public: csample(); const char **getlist(int); }; csample::csample() { } const char *csample::list1[] = {"item1" , "item2" , "item3"}; const char *csample::list2[] = {"item4" ,"item5" ,"item6"}; const char **csample::getlist(int i) { switch(i) { case 1: homecoming csample::list1; break; case 2: homecoming csample::list2; break; default: break; } } int main(int argc , const char *argv[]) { csample *osample = new csample(); const char**list1item = osample->getlist(1);//here getlist returns list1+list2 item wrong , not sure why... cout << "items list1 " << endl; while(*list1item != null) { cout << *list1item << endl; list1item++; } const char **list2item = osample->getlist(2);//whereas list2 items returned correctly using same method thought why? cout << "items list2" << endl; while(*list2item != null) { cout << *list2item << endl; list2item++; } homecoming 0; }
you need add together null terminate lists
const char *csample::list1[] = {"item1" , "item2" , "item3", null};
c++
Comments
Post a Comment