当前位置:网站首页>二维数组赋初值你会几种方法?
二维数组赋初值你会几种方法?
2022-07-25 14:38:00 【华为云】
二维数组赋初值你会几种方法
@TOC
方法一
- 分行给二维数组赋初值:
int arr[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};这种赋值方法简单直观,把第一个花括号的值给第一行,一次类推。
问:arr[2][0]=?
答:9
方法二
- 按数组排列顺序对元素赋值:
int arr[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};缺点:如果数据较多,容易遗漏,不方便检查。
问:arr[1][2]=?
答:7
方法三
- 对部分元素赋值
int arr[3][4]={{1},{5},{9}};未赋初值的元素默认为0。
优点:适用于数据多且零较多的情况,不需要把每个零标出,只需要输入少量的数据。
问:arr[2][3]=?
答:0
方法四
- 对全部元素赋初值时,可以省略第一维的长度,系统会根据数据总个数判断第二维的长度,但第二维的长度不可省略。
int arr[][4]={1,2,3,4,5,6,7,8,9,10,11,12}系统会根据数据总个数分配存储空间,一共12个数据,每行4列,当然可确定为3行。
问:arr[2][2]=?
答:11
方法五
- 在对部分元素赋初值时,也可以省略一维的长度,但是需要分行赋初值。
int arr[][4]={{1,2,3,4},{},{9,10,11,12}};问:arr[1][2]=?
答:0
边栏推荐
- Is it safe for Guolian securities to buy shares and open an account?
- Gameframework making games (I)
- The main function of component procurement system, digital procurement helps component enterprises develop rapidly
- sqli-labs Basic Challenges Less11-22
- Matplotlib data visualization three minutes entry, half an hour enchanted?
- D2. Chopping Carrots (Hard Version) (每日一题)
- I2C设备驱动程序的层次结构
- 应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
- QObject source code analysis -d pointer and Q pointer
- filters获取data中的数据;filters使用data中的数据
猜你喜欢

河源市区推出消防安全主题奶茶 助推夏季火灾防控

How to design a high concurrency system?

sqli-labs Basic Challenges Less11-22

【MySQL必知必会】触发器 | 权限管理

Go语言创始人从Google离职

The main function of component procurement system, digital procurement helps component enterprises develop rapidly

机械制造业数字化新“引擎”供应链协同管理系统助力企业精细化管理迈上新台阶

English语法_不定代词 - other / another

The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level

MySQL table operation
随机推荐
LeetCode-198-打家劫舍
[MySQL series] - how much do you know about the index
gson与fastjson
Awk from entry to earth (24) extract the IP of the instruction network card
PT100 temperature measurement circuit diagram (AD590 typical temperature measurement circuit)
Huawei ENSP router static route (the next hop address of the default route)
Gson and fastjson
awk从入门到入土(23)awk内置变量ARGC、ARGC--命令行参数传递
【cartographer_ros】八: 官方Demo参数配置和效果
The concept and operation rules of calculus of variations
华为ensp路由器静态路由(默认路由的下一跳地址)
English语法_不定代词 - other / another
Gameframework making games (I)
Go language founder leaves Google
English grammar_ Indefinite pronoun - other / other
安防市场进入万亿时代,安防B2B网上商城平台精准对接深化企业发展路径
Two Sum
QObject源码剖析-d指针和q指针
Sqli labs installation environment: ubuntu18 php7
D2. picking carrots (hard version) (one question per day)