当前位置:网站首页>c语言:5、多维数组
c语言:5、多维数组
2022-07-27 16:47:00 【兔头哥哥】
文章参考:https://wangdoc.com/clang/array.html
1、定义数组格式
数据类型 数组名[长度1][长度2]...[长度n]
int num[3][3] = {
{
1,2,3}, {
1,2,3}, {
1,2,3}};
把上面的数组看作一个3x3的矩阵,如下图:
2、初始化数组方式
方式1:
数据类型 数组名[长度1][长度2]...[长度n] = {
{值1,..., 值n},...{值1, ..., 值n}};
方式2:
数据类型 数组名[长度1][长度2]...[长度n];
数组名[下标1]...[下标n] = 值;
注意:
采用方式1初始化数组时,必须指定列的维数,因为系统会根据数组中元素的总个数来分配空间,当知道元素总个数以及列的维数后,会直接计算出行的维数。
采用第二种初始化时,数组必须同时指定行和列的维数。
#include <stdio.h>
int main()
{
//使用第一种方式初始化方式声明并初始化二维数组arr1
int arr1[][3] = {
{
1,2,3},{
1,2,3},{
1,2,3}};
//使用第二种方式初始化方式声明并初始化二维数组arr2
int arr2[3][3];
return 0;
}
3、数组作为函数参数
因为C语言没有获取数组长度的方法,因此数组作参数一般都需要传入数组长度
3.1、一维数组作参数
int ArraySum(int array[], int length){
int sum = 0;
for(int i=0; i<length; i++){
sum += array[i];
}
return sum;
}
int main(){
int array[] = {
1,2,3};
int sum = ArraySum(array,3);
return 0;
}
3.2、多维数组作参数
多维数组作参数,除了第一维的数组长度可以用参数传递,其他的需要定义出来
int sumArray(int array[][4], int length){
int sum = 0;
for(int i=0; i<length; i++){
for(int j=0; j<4; j++){
sum += array[i][j];
}
}
return sum;
}
int arr[4][4] = {
{
1,2,3,4}, {
1,2,3,4}, {
1,2,3,4}, {
1,2,3,4}, };
int sumNo = sumArray(arr,4);
n维数组作参数
int sumMulArr(int array[][3][3], int length){
// ...
return 0;
}
3.3、变长数组作参数
下方例子中数组的长度取决于参数length,只有运行时才知道,所以参数length的位置一定要在参数array前面,否则会报错
void SumArray(int length, int array[length]){
//...
}
因为函数原型可以省略参数名,所以变长数组的原型中,可以使用*代替变量名,也可以省略变量名。
int SumArray(int, int [*]);
int SumArray(int, int []);
3.4、数组字面量作为参数
//数组变量作为参数
int a[] = {
2,3,4,5};
int sum = sum_array(a, 4);
//数组字面量作为参数
int sum = sum_array((int []){
2,3,4,5}, 4);
上方实例中,两种写法等价。第二种写法省略了数组变量的声明,直接将数组字面量传入函数,{2,3,4,5}是数组值的字面量,(int [])类似于强制的类型转换,告诉编译器怎么理解这组值
边栏推荐
- Self control principle learning notes - system stability analysis (2) - loop analysis and Nyquist bode criterion
- [Luogu p4183] cow at large P (graph theory) (tree array)
- Kettle references external scripts to complete phone number cleaning, de duplication and indentation
- How to generate random numbers with standard distribution or Gaussian distribution
- Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
- Matrix of shell programming -- it's cute and cool
- What if idea successfully connects to the database without displaying the table
- 请问创建MySQL数据源资源组必须要选择新建独享数据集成资源组才可用?还是使用公共资源组就可以?谢谢
- Technology Summit 58 Liu Yuan in the same city was invited to attend qecon 2022 global software quality & effectiveness conference
- 【云图说】 第250期 初识华为云微服务引擎CSE
猜你喜欢

ES6学习笔记(1)——快速入门

C # interaction with MySQL database - MySQL configuration and addition, deletion, query and modification operations

VMware: set up SSH

IDEA成功连接Database但不显示表怎么办

Self control principle learning notes - system stability analysis (1) - BIBO stability and Routh criterion

图的遍历的定义以及深度优先搜索和广度优先搜索(二)

Summary of "performance test" of special test

Mongodb learning notes (1) - install mongodb and its related configurations

5W奖金池/面向高校,2022法律科技创新大赛报名火热进行中

MongoDB学习笔记(1)——安装MongoDB及其相关配置
随机推荐
go-zero单体服务使用泛型简化注册Handler路由
Questions about webservice
Usage of ref keyword
Nacos的基本使用(1)——入门
电商商城小程序项目完整源码(微信小程序)
An experience
成本高、落地难、见效慢,开源安全怎么办?
阿里云视频点播服务的开通和使用
C language preprocessing instruction
Memory management A4
MySQL learning notes (2) -- stored procedures and stored functions
PHP string operation
IDEA成功连接Database但不显示表怎么办
[cloud picture theory] the first time to know Huawei cloud micro service engine CSE in issue 250
SQL field type conversion
汉字查拼音微信小程序项目源码
200行代码快速入门文档型数据库MonogoDB
Resource for NS2 beginner
4 轮拿下字节 Offer,面试题复盘
阿里云对象存储OSS的开通和使用