这个C++代码是如何工作的

How is this C++ code working

本文关键字:工作 何工作 C++ 代码 这个      更新时间:2023-10-16

我正在处理一个现有的项目(由其他人编写),我无法理解这两个简单的函数。

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

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

所以我可以看到iCounts被传递到GetGrades,这很好。但后来iCounts被这样使用:

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

所以有些事情改变了iCounts。但当我查看GetGrades时,它看起来是这样的:

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是如何被修改的?

数组衰减为指针;当您向函数传递一个时,您传递的不是整个数组的副本,而是指向数组的指针。