当前位置:网站首页>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 [])类似于强制的类型转换,告诉编译器怎么理解这组值
边栏推荐
- Kettle references external scripts to complete phone number cleaning, de duplication and indentation
- 落实责任到人,广州多措并举筑牢儿童暑期“安全线”
- Unity learning notes - six common functions of object movement
- Nacos的基本使用(1)——入门
- Questions about webservice
- 正十七边形尺规作图可解性复数证明
- 低代码实现探索(四十五)业务参数
- 编程式跳转
- Kettle separate and merge records
- Down sampling - signal phase and aliasing
猜你喜欢

电商商城小程序项目完整源码(微信小程序)

Daily question (02): inverted string

mysql学习笔记(1)——变量

Basic use of Nacos (1) - getting started

Basic network faults and troubleshooting

kettle EXCEL 累计输出数据

kettle 分列、合并记录

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?

kettle JVM内存设置---效果不明显

4 轮拿下字节 Offer,面试题复盘
随机推荐
IPFs obtains the public key and private key through the interface, and encrypts the storage. First bullet
The go zero singleton service uses generics to simplify the registration of handler routes
一个经验
mysql学习笔记(1)——变量
Latex use - control the display position of tables or graphics
asp. Net experience
编程式跳转
SQL server top 关键字使用
kettle EXCEL 累计输出数据
go-zero单体服务使用泛型简化注册Handler路由
一篇让你掌握线程和线程池,还解决了线程安全问题,确定不看看?
我想咨询下,我们的maxcompute spark程序需要访问redis,开发环境和生产环境redi
WSN journal indexed by SCI
Unity learning notes (rigid body physics collider trigger)
VMware: set up SSH
Electromagnetic field learning notes - vector analysis and field theory foundation
Kettle learning - the repository configuration in version 8.2 is grayed out, and there is no connect button
ipfs通过接口获得公钥、私钥,并加密存储。第一弹
c语言:13、指针与内存
Usage of ref keyword