C语言程序设计数组和算法基础
今天我们学习C语言程序设计第6章——数组和算法基础的内容。今天的主要内容有三部分,分别是一维数组的定义和初始化,二维数组的定义和初始化,向函数传递一维数组。
Today we are studying C Programming Chapter 6 - Arrays and Algorithmic Foundations. There are three main parts to today"s topic: defining and initialising one-dimensional arrays, defining and initialising two-dimensional arrays, and passing one-dimensional arrays to functions.
No. 1 一维数组的定义和初始化
数组是一组具有相同类型的变量的集合,它是一种顺序存储,随机访问的顺序表结构。数组名就是用来标识这组相同类型的数据。构成数组的每个元素项称为数组元素。C程序通过数组的下标实现对数组元素的访问。
一维数组的一般定义格式为:
类型 数组名【元素个数】;
An array is a collection of variables of the same type; it is a sequential table structure stored in order and accessed randomly. The name of the array is used to identify the set of data of the same type. Each element of an array is called an array element, and C programs access the elements of an array by means of its subscripts.
The general definition of a one-dimensional array is in the following format
type array name [number of elements].
No. 2二维数组的定义和初始化
二维数组的一般定义格式为:
类型 数组名【第一维长度】【第二维长度】;
第一维的长度代表数组每一列的元素个数,第二维的长度代表数组每一行的元素个数。例如:short matrix[3][4];
一维数组在内存中占用的字节数为:数组长度*sizeof(基类型),二维数组占用的字节数为:第一维长度*第二维长度*sizeof(基类型)
The general format of the definition of a two-dimensional array is
type array name [length of the first dimension] [length of the second dimension].
The length of the first dimension represents the number of elements in each column of the array, and the length of the second dimension represents the number of elements in each row of the array. Example: short matrix[3][4].
The number of bytes occupied by a one-dimensional array in memory is: array length * sizeof (base type), the number of bytes occupied by a two-dimensional array is: length of the first dimension * length of the second dimension * sizeof (base type)
No. 3向函数传递一维数组
数组元素和基本型变量一样,既可出现在任何合法的C表达式中,也可用作函数参数。
若要把一个数组传递给一个函数,那么只要使用不带括号的数组名作为函数实参调用函数即可。
由于数组名代表数组第一个元素的地址,因此用数组名作函数实参实际上是将数组的首地址传给被调函数。
Array elements, like basic variables, can appear in any legal C expression and can also be used as function parameters.
To pass an array to a function, simply call the function using the array name without parentheses as a function reference.
As the array name represents the address of the first element of the array, using the array name as a function reference is in effect passing the first address of the array to the function being called.
注意:1.C语言中数组的下标都是从0开始的。
2.在不同编译系统中,int型所占的字节数是不同的。使用sizeof运算符来计算一个类型或变量在内存中所占的字节数更利于提高程序的可移植性。
3.数组第二维的长度声明永远都不能省略。
4.数组做函数形参时,数组的长度可以不出现在数组名后面的方括号内,通常用另一个整型形参来指定数组的长度。
Note: 1. The subscripts of arrays in C all start from 0.
2. The number of bytes occupied by an int type varies from one compilation system to another. It is better to use the sizeof operator to calculate the number of bytes in memory for a type or variable to improve program portability.
3. The length declaration of the second dimension of an array should never be omitted.
4. When an array is used as a formal reference to a function, the length of the array may not appear in the square brackets after the array name, but is usually specified by another integer formal reference.
参考资料:文字:百度;图片:微博;翻译:百度翻译
本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。
从火遍大江南北到被批节目不行?河南卫视这就江郎才尽了文令狐伯光今年因为中国娱乐圈整顿的关系,后来首个重大晚会中秋晚会可以说是万众瞩目了,其中年初就不用什么明星,而以创意,诚意十足的传统节目晚会的河南卫视,可以说在2021年卫视晚会中
南社划痕散江南爷爷的墓地在迁入灵岩山安息公墓前是在虎丘后山的,有世代熟悉的农民看护,小的时候家族的孩子们祭拜完祖宗后总要去虎丘玩,小孩子喜欢一群人在树林里玩捉迷藏,偶然发现路旁有个大墓,圆形的,
追思江南一代高僧明学明学长老圆寂已经五年了,让我们一起来回顾一下明学的点滴往事吧!身为灵岩山寺方丈的明学长老,他在寺内或外出,常有人给他供养红包,若据为己有也无人过问,然而他却分文不动,全部上缴库房,
光哥拍案评江苏南通车主贴731必胜光哥家事国事八卦事,事事关心!风声雨声聒噪声,声声入耳!现在升堂啪(惊堂木拍起来)左右升堂光哥左右!有事上奏,无事退堂喝酒!牛头启禀光哥,近日江苏南通有人在车上贴731部队,必胜的
2021浙江卫视招商会片单解析19部综艺14部剧集犀牛娱乐原创文冷罐头编辑夏添进入四月,各大卫视平台相继召开招商会。本周内,优酷芒果TV先后在招商会上发布了2021年片单,激起了不小的讨论声量。平台的剧集储备情况和战略打法,引发了
全靠同行的衬托?刘德华新片竟成2020年商业大片最佳文令狐伯光邱礼涛导演,刘德华刘青云倪妮等主演的警匪片拆弹专家2。自12月24日上映,截止本帖发表前票房近4亿。现在某平台预测票房是10亿,但是考虑到最近没有什么重磅大片上映。同时已
首日票房破亿!这部2020年末重磅国产救市大片,有这2点底气文令狐伯光林超贤导演,彭于晏王彦霖辛芷蕾蓝盈莹等主演的电影紧急救援。已于12月18日正式上映,首日票房破亿。这个成绩,即使是放在往年,也是相当不错的成绩。而在2020年,它更是难能
宋仲基主演,特效亚洲第一?韩国首部科幻大片不输流浪地球文令狐伯光最近韩国因为文化剽窃中国文化事件,在中国互联网闹得沸沸扬扬的。但发展成熟的韩国流行文化产业,又是很多中国网友羡慕的地方。国内多数网友对于团体偶像KPOP韩国综艺和韩剧等看
中国特效大片打了多少观众的脸?我们看刺杀小说家就知道了文令狐伯光宁浩监制,路阳导演,雷佳音杨幂董子健于和伟郭京飞等主演的刺杀小说家。已于2021年2月12日大年初一上映,但是电影票房表现不好。12日上映时首天票房破了亿,但是现在上映4
哥斯拉大战金刚的觉悟,能让多少好莱坞大片脸红?文令狐伯光亚当温佳德导演,亚历山大斯卡斯加德米莉博比布朗丽贝卡豪尔布莱恩泰里亨利小栗旬等联合主演的哥斯拉大战金刚。已经于3月26日正式上映,这部好莱坞大片还是非常受到中国观众喜欢的
文章马伊琍离婚婚姻里,这三件事越早知道越好前两天,文章马伊琍离婚的消息瞬间刷爆网络。文章马伊琍相识于2005年拍摄锦衣卫,从热恋到婚后狂撒狗粮,再经历文章出轨,马伊琍选择原谅,到今天,两人的婚姻还是没能坚持下来。曾今那么幸