使用指针算法在数组上使用c ++ App

时间:2017-05-04 05:20:17

标签: c++ arrays pointers

问题:如何使用该函数从数组中生成随机动物?

const int MAX =12;       //12 animals

const int MAXSTR = 10;   //Max length for set

typedef char * Element;
static Element UniversalSet [MAX] = {"Rat", "Ox", "Tiger", "Rabbit", "Dragon","Snake", "Sheep", "Monkey", "Rooster", "Dog", "Pig", "Horse"};

void constructSet (Element *, int size)
{   
    Element* s;

    for (int i = 0; i <= size; i++) 
    {
         s[i] = UniversalSet [rand() % 12];
    }
        cout << "Set S = {" << s[i] << "}";

}

1 个答案:

答案 0 :(得分:1)

语法错误移动循环中的cout

  void constructSet(Element * elements, int size)
{
    Element* s = elements;

    for (int i = 0; i <= size; i++)
    {
        s[i] = UniversalSet[rand() % 12];

        cout << "Set S = {" << s[i] << "}";//Here
    }

}

然后尝试这个,它会起作用:

Element e[3] ;
constructSet(e, 3);