Cainv89
2018-01-24 13:07
采纳率: 50%
浏览 1.2k

C++指针数组传参问题,小白一枚望大神回帖,谢谢~

struct ITEM
{
unsigned int id_;
unsigned short level_;
};

void reset(ITEM* item[])
{

for (unsigned short i = 0; i < 3; i++)
{
item[i]->level_ = 3-i;
cout << item[i]->id_ << " " << item[i]->level_ << endl;
}
}

int main()
{
ITEM* item[10];
for (unsigned short i = 0; i < 10; i++)
{
ITEM tmp;
tmp.id_ = i;
tmp.level_ = i;
item[i] = &tmp;
cout << item[i]->id_ << " " << item[i]->level_ << endl;
}
reset(item);
system("pause");
return 0;

}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • hany2008 2018-01-24 15:19
    已采纳

    #include
    using namespace std;
    struct ITEM
    {
    unsigned int id_;
    unsigned short level_;
    };
    void reset(ITEM item[])
    {
    for (unsigned short i = 0; i < 10; i++) //验证传入的数据的正确性
    {
    cout << item[i].id_ << " " << item[i].level_ <<"111"<< endl;
    }
    for (unsigned short i = 0; i < 3; i++)
    {
    item[i].level_ = 3-i;
    cout << item[i].id_ << " " << item[i].level_ <<"222"<< endl;
    }
    for (unsigned short i = 0; i < 10; i++) //验证修改数据后的正确性
    {
    cout << item[i].id_ << " " << item[i].level_ <<"333"<< endl;
    }
    }
    int main()
    {
    ITEM item[10];
    for (unsigned short i = 0; i < 10; i++)
    {

    item[i].id_ = i;
    item[i].level_ = i;
    cout << item[i].id_ << " " << item[i].level_ <<"ddd"<<endl;
    }
    reset(item);
    system("pause");
    return 0;

    }

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(5条)

相关推荐 更多相似问题