惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
量子位
aimingoo的专栏
aimingoo的专栏
V
V2EX
Vercel News
Vercel News
B
Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
S
Secure Thoughts
U
Unit 42
博客园 - 叶小钗
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
W
WeLiveSecurity
博客园 - Franky
Forbes - Security
Forbes - Security
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Schneier on Security
Schneier on Security
I
InfoQ
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
Last Week in AI
Last Week in AI
Security Archives - TechRepublic
Security Archives - TechRepublic
C
CERT Recently Published Vulnerability Notes
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
Lohrmann on Cybersecurity
SecWiki News
SecWiki News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
J
Java Code Geeks

博客园 - 撬棍

【.Net】2、8、16进制转换 【.Net】执行CMD命令 【.Net】获取随机数函数 【.Net】注册程序开机启动 【.Net】把窗体“钉”到桌面上 【.Net】多语言查看MSDN 【.Net】 显示星期字符串 【.Net】 判断时间字符串正确性 【.Net】 实现窗口拖动 【.Net】 Winform 单例运行实例 [C++]函数返回值 [C++]const的指针使用 [C++]指针类型出参 [VBA]Excel输出utf-8编码格式文件 使用WideCharToMultiByte 【C++】split [C语言学习]之打印万年历 - 撬棍 - 博客园 [VB6.0]让程序在任务列表和资源管理器“隐身” [InstallShield]FindAllFiles与SetFileInfo配合实现文件加多文件属性设置 [VB]修改注册表让程序开机自动运行 - 撬棍 - 博客园
[C++]数组参数
撬棍 · 2013-05-14 · via 博客园 - 撬棍

在C++中数组永远不会按值传递它是传递第一个元素准确地说是第0 个的指针。

例如如下声明

void putValues( int[ 10 ] );

被编译器视为

数组的长度与参数声明无关因此下列三个声明是等价的

  // 三个等价的putValues()声明
  void putValues( int* );
  void putValues( int[] );
  void putValues( int[ 10 ] );

第一种是传指针不用说了。

另外一种机制是将参数声明为数组的引用当参数是一个数组类型的引用时数组长度,
成为参数和实参类型的一部分编译器检查数组实参的长度与在函数参数类型中指定的长度是否匹配。--可能也正是因为这种限制很少使用此种方式。

// 参数为10 个int 的数组
// parameter is a reference to an array of 10 ints
void putValues( int (&arr)[10] );
int main() {
    int i, j[ 2 ];
    putValues( i ); // 错误: 实参不是 10 个 int 的数组
   putValues( j ); // 错误: 实参不是 10 个 int 的数组
   return 0;
}

因为数组的长度现在是参数类型的一部分所以putValues()的这个版本只接受10 个int的数组。

void putValues( int (&ia)[10] )
{
    std::cout << "( 10 )< ";
    for ( int i = 0; i < 10; ++i ) 
    {
        std::cout << ia[ i ];
        // 用逗号分隔元素
        if ( i != 9 )
            std::cout << ", ";
    }
    std::cout << " >\n";
}
void main()
{
    int i[10]  = {1,2,3,4,5,6,7,8,9,0};
    putValues( i ); // 错误: 实参不是 10 个 int 的数组
    return;
}

注意:*ia周围的括号是必需的因为下标操作符的优先级较高下列声明:

将ia声明成一个含有10 个指向int 的指针的数组。