2014-10-10 29 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