2014-10-10 30 views
0

我有以下代碼片段,意圖是獲得某些項目列表和打印。它正在編譯,但在運行時,輸出不如預期。我在意料之外添加了評論。請讓我知道我在這裏做錯了什麼。無法檢索靜態字符*數組

#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: 
     return cSample::list1; 
     break; 
    case 2: 
     return 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 which is wrong , I am not sure why... 
    cout << "Items from List1 " << endl; 
    while(*list1Item != NULL) 
    { 
     cout << *list1Item << endl; 
     list1Item++; 
    } 

    const char **list2Item = oSample->GetList(2);//whereas list2 items are returned correctly using same method any idea why? 
    cout << "Items from list2" << endl; 
    while(*list2Item != NULL) 
    { 
     cout << *list2Item << endl; 
     list2Item++; 
    } 
    return 0; 
} 

回答

1

您需要添加一個空終止您的清單

const char *cSample::list1[] = {"Item1" , "Item2" , "Item3", NULL}; 
+0

應該還添加了'NULL'到'list2 []'或者它是UB。 – Abhineet 2014-10-10 06:52:19

+0

你可能更喜歡'nullptr'自C++ 11以來。 – Jarod42 2014-10-10 07:32:11

1

你從來沒有列表數組的最後一個元素設置爲NULL,和你保持遞增list1Item。其實,你在做什麼是UB

只需添加一個NULL,以避免遇到一樣的行爲,

const char *cSample::list1[] = {"Item1" , "Item2" , "Item3", 0}; 
const char *cSample::list2[] = {"Item4" ,"Item5" ,"Item6", 0}; 
0

你希望你的數組爲空值終止,但他們沒有。所以當你列舉list1時,你直接跑到list2

0

就像已經提到別人,你需要空終止您的清單

#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", '\0'}; 
const char *cSample::list2[] = {"Item4" ,"Item5" ,"Item6", '\0'}; 

const char **cSample::GetList(int i) 
{ 
    switch(i) 
    { 
    case 1: 
     return cSample::list1; 
     break; 
    case 2: 
     return 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 which is wrong , I am not sure why... 
    cout << "Items from List1 " << endl; 
    while(*list1Item != NULL) 
    { 
     cout << *list1Item << endl; 
     list1Item++; 
    } 

    const char **list2Item = oSample->GetList(2);//whereas list2 items are returned correctly using same method any idea why? 
    cout << "Items from list2" << endl; 
    while(*list2Item != NULL) 
    { 
     cout << *list2Item << endl; 
     list2Item++; 
    } 
    return 0; 
} 
+0

雖然'nullptr'優先於'NULL',這裏你的零值'\ 0'不是正確的類型: - /。 – Jarod42 2014-10-10 07:31:30

+0

內部表示爲0xDEADBEEF的空指針仍然是空指針,不管它的位串是什麼樣子,它仍然會比較等於NULL,0,\ 0和所有其他空指針常量形式。 – dev0 2014-10-10 07:37:01

+0

我不認爲你的回答是不正確的,只是它是誤導性的,爲什麼使用** char **'\ 0'而不是正確的類型? – Jarod42 2014-10-10 07:44:04