知识点3:数组(arrays)

1

Arrays are a really fundamental data structure for any programming language that you will use.

We use arrays to hold values of the same data type at contiguous memory locations.

That is to say, it's a way that we can group a bunch of integers together in memory or a bunch of characters or floats in memory really close together and work with them without having to give each one its own unique name, which can get cumbersome after a little while.

Now, one way to analogize arrays is to think about your local post
office for a second.

Usually, in most post offices, there's a large bank a post office boxes on the wall.

2

An array is a giant block of contiguous memory, the same way that a mail bank in your post office is a large space on the wall of the post office. Arrays have been partitioned into small, identically sized blocks of space, each of which is called an element, in the same way that the wall of the post office has been partitioned into small, identically sized blocks of space, which we call a PO box.

Each element of the array can store a certain amount of data, just as each post office box is able to hold a certain amount of mail.

3

What can be stored in each element of the array is variables of the same data type, such as int or char, just like in your post office box,
you can only fit things of a similar type,
such as letters or small packages.

4

Lastly, we can access each element of the array directly by index number, just as we can access our post office box by knowing its mailbox number.

5

In C, the elements of an array are indexed starting from 0, not from 1.

So if an array consists of n elements, the first element of that array
is located at index 0, and the last element of the array is located at index (n - 1).

Unfortunately, or fortunately, depending on your perspective, C is very lenient(宽大) here. It will not prevent you from going out of bounds of your array.

You could access the minus 3 element of your array or the 59th element of your array, if your array only has 50 elements.

It won't stop your program from compiling, but at run time, you might encounter a dreaded segmentation fault if you start to access memory that is outside the bounds of what you asked your program to give you.

So do be careful.

6

What does an array declaration look like?

How do we code an array into existence like we code any other variable?

Array declarations:

type name[size];

There are three parts to an array declaration-- a type, a name, and a size.

This is very similar to a variable declaration, which is just a type and a name, the size element being the special case for an array, because we are getting a bunch of them at the same time.

7

Arrays are not restricted to a single dimension, which is pretty cool. You can actually have as many side specifiers as you wish.

for example:

type name[size][size];

And then, you can choose to interpret this in your mind as a (size) by (size) grid of cells.

Now, in fact, in memory, it really does just remain a (size*size) element, single dimensional array.

8

Now, something really important about arrays.

We can treat each individual element of the array as a variable. But we can't treat entire arrays themselves as variables.

We cannot, for example, assign one array to another array using the assignment operator. It's not legal C.

If we want to, for example-- what we would be doing in that example would be to copy one array into another. If we want to do that, we actually need to use a loop to copy over each individual element one at a time.

The correct way:

In other programming languages, more modern ones, you can, in fact, do just that simple equals syntax.

But C, unfortunately, we're not allowed to do that.

9

Now, there's one other thing I want to mention about arrays that can be a little bit tricky the first time you work with them.

We discussed in a video about variable scope, that most variables in C, when you call them in functions, are passed by value.

Do you remember what it means to pass something by value?

It means we're making a copy of the variable that's being passed in.

The callee function, the function that's receiving the variable, doesn't get the variable itself. It gets its own local copy of it to work with.

Arrays, of course, do not follow this rule.

Rather, what we call this is passing by reference. The callee actually does receive the array. It does not receive its own local copy of it.

And if you think about it, this makes sense.

If arrays are really large, it takes so much time and effort to make a copy of an array of 100 or 1,000 or 10,000 elements, that it's not worth it for a function to receive a copy of it, do some work with it, and then just be done with the copy; it doesn't need to have it hanging around anymore.

Because arrays are some bulky and cumbersome, we just pass them by reference.

We just trust that function to, don't break anything. So it does actually get the array. It doesn't get its own local copy of it.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,976评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,249评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,449评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,433评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,460评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,132评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,721评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,641评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,180评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,267评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,408评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,076评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,767评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,255评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,386评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,764评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,413评论 2 358

推荐阅读更多精彩内容