You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
固定尺寸版本:constexpr void transpose_2x3(const int (&matrix)[2][3], int (&result)[3][2]),用数组引用作形参把维度纳入类型系统,行数列数不匹配会在编译期报错。
C99 VLA 写法(明确标注"不是标准 C++"):先展示 void transpose(int m, int n, const int matrix[m][n], int result[n][m]) 这种变长数组形参,再用一段文字讲清它的标准归属——C99 引入、C11 起降级为可选特性、不被任何 C++ 标准接受,编译器接受也属于扩展,不能写进可移植的 C++ 代码。这直接回答了题面"如果行数列数也要是参数,C 风格数组能做到吗"的思考题。
固定尺寸版本:constexpr void transpose_2x3(const int (&matrix)[2][3], int (&result)[3][2]),用数组引用作形参把维度纳入类型系统,行数列数不匹配会在编译期报错。
C99 VLA 写法(明确标注"不是标准 C++"):先展示 void transpose(int m, int n, const int matrix[m][n], int result[n][m]) 这种变长数组形参,再用一段文字讲清它的标准归属——C99 引入、C11 起降级为可选特性、不被任何 C++ 标准接受,编译器接受也属于扩展,不能写进可移植的 C++ 代码。这直接回答了题面"如果行数列数也要是参数,C 风格数组能做到吗"的思考题。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
投稿类型
documents/community/incoming/文章说明
概述
本 PR 为第一卷第 5 章第 1 篇《C 风格数组》(
documents/vol1-fundamentals/ch05/01-c-arrays.md)的「动手试试」小节补全三道练习的参考答案,延续 #201、#206、#207 的练习答案补全系列,格式与既有答案保持一致:使用 VitePress::: details 参考答案折叠块,正文题面保持不变,读者可先自行作答再展开对照。✅ 完成内容
练习一:数组求和与均值
constexpr int sum(const int a[], int n)与constexpr double average(const int a[], int n)两个函数,返回类型按题面要求区分int/double。average对n == 0 || n < 0做了防御性处理:避免常量求值中出现除以零(否则constexpr调用直接编译失败),也避免负数长度被误用。constexpr变量接收结果,演示了这套写法可以在编译期完成计算;附编译命令与运行结果(总和55、平均值5.5),读者可按题面要求手动验算对照。练习二:矩阵转置(本题是本次的重点,题面原本只留了思考题,答案分三层展开)
constexpr void transpose_2x3(const int (&matrix)[2][3], int (&result)[3][2]),用数组引用作形参把维度纳入类型系统,行数列数不匹配会在编译期报错。void transpose(int m, int n, const int matrix[m][n], int result[n][m])这种变长数组形参,再用一段文字讲清它的标准归属——C99 引入、C11 起降级为可选特性、不被任何 C++ 标准接受,编译器接受也属于扩展,不能写进可移植的 C++ 代码。这直接回答了题面"如果行数列数也要是参数,C 风格数组能做到吗"的思考题。rows * cols个元素)和改进方向(模板表达编译期维度,或改用std::vector/std::span表达运行时尺寸)。此外将本题题面从"如果行数列数也要是参数,C 风格数组能做到吗?"微调为"如果行数、列数要在运行时传入,标准 C++ 应该如何表示这块矩阵内存?",把问题从"能不能"引导到"标准 C++ 里该怎么做",与答案的展开方式呼应;同时统一了乘号的排版(
N x M→N × M)。练习三:修复越界 bug
i <= 5改为i < 5,消除对data[5]的越界读(未定义行为),并与正文"最大合法下标是 N−1"的结论呼应。Frontmatter
reading_time_minutes: 10→13,反映新增的三段参考答案带来的阅读量变化。📝 技术亮点
constexpr,体现 C 风格数组 + 编译期计算的组合。const int (&)[2][3])保留完整类型信息,规避了"数组退化为指针、丢失维度"的经典坑,与正文知识点形成闭环。std::span等后续学习方向。📄 变更文件
documents/vol1-fundamentals/ch05/01-c-arrays.md(+131 / −2,仅此一个文件)作者与授权
建议位置
保持原位:
documents/vol1-fundamentals/ch05/01-c-arrays.md。不涉及新文章收录,无需更新导航或索引。测试
测试环境:WSL2(Ubuntu 24.04,内核
6.6.87.2-microsoft-standard-WSL2),编译器g++/gcc 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04.1)。-std=c++17 -Wall -Wextra实际编译,零警告,运行结果与文中标注一致:总和: 55/平均值: 5.5;main验证,2×3 转置结果正确输出1 4 / 2 5 / 3 6;10 20 30 40 50,无越界。gcc -std=c99编译运行通过(印证"C99 特性"),同一份代码用g++ -std=c++17 -pedantic-errors编译报错use of parameter outside function body(印证"不是标准 C++")。.venv python scripts/validate_frontmatter.py→ 991/991 valid。markdownlint documents/vol1-fundamentals/ch05/01-c-arrays.md0 错误。::: details折叠块为 VitePress 支持语法,站点渲染正常,与 docs(cpp): 补充 ch03 参数传递练习参考答案 #201/docs(cpp): 补全 inline constexpr 练习答案 #206/docs(cpp): 补全指针基础练习答案 #207 同类 PR 格式一致。检查项
关联 Issue / Discussion
Close #
Related #