当前位置:网站首页>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 [])类似于强制的类型转换,告诉编译器怎么理解这组值
边栏推荐
- Anaconda下安装Talib库
- c语言:13、指针与内存
- Usage of ref keyword
- Latex use - subfigure vertical graphics
- Kinect for unity3d - backgroundremovaldemo learning
- Webmagic+selenium+chromedriver+jdbc垂直抓取数据。
- An experience
- 又有一个Repeater的例子
- 200行代码快速入门文档型数据库MonogoDB
- What if idea successfully connects to the database without displaying the table
猜你喜欢

c语言:12、gdb工具调试c程序

Subscription and use of Alibaba cloud video on demand service

An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?

汉字查拼音微信小程序项目源码

sql 字段类型转换

Kettle consolidated record data reduction

SQL field type conversion

Kettle separate and merge records

Unity learning notes (rigid body physics collider trigger)

Performance analysis of continuous time system (1) - performance index and first and second order analysis of control system
随机推荐
idea优化小攻略
SSM project uses filter to realize login monitoring
Introduction to assembly language (1)
Normal distribution, lognormal distribution, generation of normal random numbers
搭建阿里云+typora+Picgo图床错误分析
Basic network faults and troubleshooting
Power control
JS common utils encapsulation
asp.net 的经验
Nacos基本概念和单机部署
新系统安装MySQL+SQLyog
Code interview of Amazon
ref 关键字的用法
Mongodb learning notes (1) - install mongodb and its related configurations
Resource for NS2 beginner
MongoDB学习笔记(1)——安装MongoDB及其相关配置
VIVO应用市场APP上架总结
Basic use of Nacos (1) - getting started
C language printing diamond
Sword finger offer17- print from 1 to the maximum n digits - Analog