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版)
指针
|
143
指针
编译器会在必要的地方把
void
指针转换为对象指针。例如,在下面的语句中,函数
malloc()
返回一个
void
指针,它的值是已分配内存的语句块的地址。这样的赋值操作会
把
void
指针转换成
int
指针:
int *iPtr = malloc( 1000 * sizeof(int) );
更加详细的说明,请参见例
2-3
。
9.1.3 初始化指针
具有动态存储周期的指针变量,开始是没有定义值的,除非它们在声明的同时进行了显
式地初始化。在语句块内定义的所有变量,只要没有被声明为
static
,就具有动态存储
周期。所有指针在定义时如果没有初始化,则空指针默认作为它们的初始化值。
可以用下列方式来初始化一个指针:
•
一个空指针常量
•
指向相同类型的指针,或者指向具有较少限定符修饰的相同类型(请参见
9.3
节)
•
如果需初始化的指针不是函数指针,可以使用
void
指针进行初始化(同上,需初始
化的指针可以是指向具有更多限定符类型的指针)
指针如果不具有动态存储周期,则必须用常量表达式来初始化,例如取址运算的结果,
或者使用数组或函数的名称。
当初始化指针时,除非是上面说明的情况,否则不会发生隐式类型转换。然而,可以将
一个指针值显式地转换成另一个指针类型。例如,若想一个字节接着一个字节地读取对
象,可以将其地址转换成
char
指针,指向此对象的第一个字节:
double x = 1.5;
char *cPtr = &x; //
错误:类型不匹配;没有隐式转换
char *cPtr = (char ...
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