此C ++代码如何工作 [英] How is this C++ code working

查看:59
本文介绍了此C ++代码如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个现有的项目(由其他人编写),我无法理解这两个简单的功能.

I'm working on an existing project (written by someone else) and I can't get my head around these two simple functions.

我感兴趣的第一个函数包含以下内容:

The first function I am interested in contains this:

int iCounts[NUM_GRADES];
PROFILEMAN->GetMachineProfile()->GetGrades( pSong, GAMESTATE->GetCurrentStyle()->m_StepsType, iCounts );

所以我可以看到iCounts被传递给GetGrades,这很好.但是后来在iCounts上这样使用:

So I can see that iCounts is passed in to GetGrades, which is fine. But then later on iCounts is used like this:

AppendOctal( iCounts[g], 3, foo );

因此iCounts发生了一些变化.但是当我查看GetGrades时,它看起来像这样:

So something has changed iCounts. But when I go look at GetGrades, it looks like this:

void Profile::GetGrades( const Song* pSong, StepsType st, int iCounts[NUM_GRADES] ) const{
    SongID songID;
    songID.FromSong( pSong );

    memset( iCounts, 0, sizeof(int)*NUM_GRADES );

    ...then some more stuff is done to iCounts

}

我不明白的是,当在GetGrades中不涉及指针时,如何修改原始函数的iCounts?

What I can't understand is, how does the original function's iCounts get modified when in GetGrades there are no pointers involved?

推荐答案

数组衰减到指针;当您将一个函数传递给函数时,您并没有传递整个数组的副本,而是传递了指向该数组的指针.

Arrays decay to pointers; when you pass one to a function you're not passing a copy of the whole array, but rather a pointer to the array.

这篇关于此C ++代码如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆