当前位置:网站首页>C language: 5. Multidimensional array
C language: 5. Multidimensional array
2022-07-27 19:33:00 【Brother rabbit head】
c Language :5、 Multidimensional arrays
Article reference :https://wangdoc.com/clang/array.html
1、 Define array format
data type Array name [ length 1][ length 2]...[ length n]
int num[3][3] = {
{
1,2,3}, {
1,2,3}, {
1,2,3}};
Think of the above array as a 3x3 Matrix , Here's the picture :
2、 Initialize array mode
The way 1:
data type Array name [ length 1][ length 2]...[ length n] = {
{ value 1,..., value n},...{ value 1, ..., value n}};
The way 2:
data type Array name [ length 1][ length 2]...[ length n];
Array name [ Subscript 1]...[ Subscript n] = value ;
Be careful :
Adopted by 1 When initializing an array , The dimension of the column must be specified , Because the system allocates space according to the total number of elements in the array , When you know the total number of elements and the dimension of the column , The dimension of travel will be calculated directly .
When using the second initialization , Arrays must specify both row and column dimensions .
#include <stdio.h>
int main()
{
// Use the first method to declare and initialize a two-dimensional array arr1
int arr1[][3] = {
{
1,2,3},{
1,2,3},{
1,2,3}};
// Use the second method to declare and initialize the two-dimensional array arr2
int arr2[3][3];
return 0;
}
3、 Arrays as function arguments
because C The language has no way to get the length of an array , Therefore, array length is generally required to be passed in as a parameter
3.1、 One dimensional array as parameter
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、 Multidimensional array as parameter
Multidimensional array as parameter , In addition to the first dimension, the array length can be passed with parameters , Others need to be defined
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 Dimension group as parameter
int sumMulArr(int array[][3][3], int length){
// ...
return 0;
}
3.3、 Variable length array as parameter
The length of the array in the following example depends on the parameter length, Only the runtime knows , So parameters length The position of must be in the parameter array front , Otherwise, an error will be reported
void SumArray(int length, int array[length]){
//...
}
Because the function prototype can omit the parameter name , So in the prototype of variable length array , have access to * Instead of the variable name , You can also omit the variable name .
int SumArray(int, int [*]);
int SumArray(int, int []);
3.4、 Array literal as a parameter
// Array variables as parameters
int a[] = {
2,3,4,5};
int sum = sum_array(a, 4);
// Array literal as a parameter
int sum = sum_array((int []){
2,3,4,5}, 4);
In the above example , The two expressions are equivalent . The second method omits the declaration of array variables , Directly pass the array literal into the function ,{2,3,4,5} Is the literal value of the array ,(int []) Similar to forced type conversion , Tell the compiler how to understand this set of values
边栏推荐
- 利用 Fastjson json (简单粗暴版)
- mysql学习笔记(1)——变量
- Basic concepts of Nacos and single machine deployment
- Basic knowledge of C language (for personal use)
- Subscription and use of Alibaba cloud video on demand service
- HDU1573 X问题【一元线性同余方程组】
- 搭建阿里云+typora+Picgo图床错误分析
- sql 字段类型转换
- 一篇让你掌握线程和线程池,还解决了线程安全问题,确定不看看?
- VIVO应用市场APP上架总结
猜你喜欢

C语言案例:密码设置及登录> 明解getchar与scanf

Analysis of Eureka server

c语言:7、c语言多源码文件使用方法

Programming jump

2022备战秋招10W字面试小抄pdf版,附操作系统、计算机网络面试题

101. (cesium chapter) cesium particle system - snow

2022 Ningde Vocational College Teachers' practical teaching ability improvement training - network construction and management

kettle switch / case 控件实现分类处理

c语言:5、多维数组

win10小技巧(1)——转移桌面位置
随机推荐
c语言:6、指针的简单使用与注意事项
又有一个Repeater的例子
[Luogu p3175] bitwise OR (min max inclusive) (high-dimensional prefix and / FWT)
MongoDB学习笔记(1)——安装MongoDB及其相关配置
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?
成本高、落地难、见效慢,开源安全怎么办?
Programming jump
Take byte offer in four rounds and answer the interview questions
ES6 new method
Idea optimization strategy
SQL Server top keyword usage
C language case: password setting and login > clear solution getchar and scanf
VMware: set up SSH
阿里云对象存储OSS的开通和使用
Summary of "performance test" of special test
Questions about webservice
go-zero单体服务使用泛型简化注册Handler路由
编程式跳转
Using MATLAB to generate graphics for journals and conferences - plot
电商商城小程序项目完整源码(微信小程序)