Skip to Content
C语言核心技术(原书第2版)
book

C语言核心技术(原书第2版)

by Peter Prinz, Tony Crawford
August 2017
Intermediate to advanced
798 pages
17h 44m
Chinese
China Machine Press
Content preview from C语言核心技术(原书第2版)
129
第8章
数组
数组包含给定类型的一些对象,并将这些对象依次存储在连续的内存空间中。每个独立
的对象被称为数组的元素(
element
)。元素的类型可以是任何对象类型,但函数类型或
不完整类型不能作为数组元素(请参见
2.1
节)。
数组本身也是一个对象,其类型由它的元素类型延伸而来。更具体地说,数组的类型由
元素的类型和数量所决定的。如果一个数组的元素是
T
类型,那么该数组就称为“
T
数
组”。例如,如果元素类型为
int
,那么该数组的类型就是“
int
数组”。然而,
int
数
组类型是不完整的类型,除非指定了数组元素的数量。如果一个
int
数组有
16
个元素,
那么它就是一个完整的对象类型,即“
16
个
int
元素数组”。
8.1 定义数组
数组的定义决定了数组名称、元素类型以及元素个数。没有显式初始化操作的数组定
义,其语法如下:
类型
名称
[
元素数量
]
;
元素数量在方括号(
[]
)之间,它必须是大于
0
的整数表达式。示例:
char buffer[4*512];
这一行代码定义了一个名为
buffer
的数组,它包含
2048
个
char
类型元素。
可以利用
sizeof
运算符获取对象所占内存空间的大小。数组在内存中的空间大小总是等
于一个元素的空间大小乘以数组中元素的个数。因此,上述例子中的
buffer
数组,表达
式
sizeof(buffer)
会产生
2048 * sizeof(char)
的值。换句话说,
buffer
数组占用
2048
个内存字节,因为
sizeof(char)
等于
1
。
130
|
第
8
章
在数组定义中,可以将元素数量指定为一个常量表达式,或者在特定情况下,指定为涉 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)

Publisher Resources

ISBN: 9787111562207