{"componentChunkName":"component---node-modules-gatsby-theme-gine-blog-src-components-post-blog-post-js","path":"/posts/fd58d9d32292425eabb52ebec9a54f76","webpackCompilationHash":"abbc32fa0c65a814f292","result":{"data":{"siteConfig":{"title":"812lcl's Blog","commentDisqusShortname":"812lcl-notion-blog","commentOpen":true},"posts":{"public_date":"2013-12-17","name":"重学C语言(一)——类型转换","tags":["C","编程语言"],"html":"<div data-block-id=\"765e7ce9-5def-49a6-8a7c-f24705c62c95\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 2px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">最近又读了一下基本 C 语言经典著作，特此整理下一些 C 语言的知识点。参考书籍有：C程序设计语言、C专家编程、C缺陷与陷阱  ## 1. 整型提升 在一个表达式中，凡是可以使用整型的地方都可以使用带符号或无符号的字符、短整型或整型位字段，还可以使用枚举类型的对象。如果原始类型的所有值都可用int类型表示，则其值将被转换为int类型；否则将被转换为unsigned int类型。这一过程称为整型提升。</div></div></div></div><div data-block-id=\"14588a8a-75b0-410b-84bd-815231e7481b\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">以下面的代码为例：</div></div></div></div><div data-block-id=\"6ba6e8a6-67f3-4a25-a5a8-ffb8ec527594\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\"><span style=\"font-family:&quot;SFMono-Regular&quot;, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;line-height:normal;background:rgba(135,131,120,0.15);color:#EB5757;border-radius:3px;font-size:85%;padding:0.2em 0.4em\" data-token-index=\"0\">printf(\" %d \", sizeof'A');</span></div></div></div></div><div data-block-id=\"7c3be378-ae88-4440-ae86-5588b530ad4f\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">这个代码本想打印出一个字符字面值类型的长度，但你会发现结果是4（或是你机器上int的长度）。这里就根据提升规则，由char转换为int。</div></div></div></div><div data-block-id=\"87e47349-b5d1-40c4-83a5-9296a187376e\" class=\"notion-selectable notion-sub_header-block\" style=\"width: 100%; max-width: 608px; margin-top: 1.4em; margin-bottom: 1px; color: rgb(55, 53, 47);\"><div style=\"display: flex; width: 100%; font-weight: 600; font-size: 1.5em; line-height: 1.3; color: inherit; fill: inherit;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\"Heading 2\" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">2. 整型转换</div></div></div><div data-block-id=\"4eff0603-03d0-4195-99f8-1ce2c4bd537e\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">将任何整数转换为某种指定的无符号类型数的方法是：以该无符号类型能够表示的最大值加1为模，找出与此整数同余的最小的非负值。在对二的补码表示中，如果该无符号类型的为模式比较窄，这就相当于左截取；如果该无符号类型的为模式比较宽，这就相当于对带符号值进行符号扩展和对无符号值进行0填充。</div></div></div></div><div data-block-id=\"5c54814e-ba68-487f-9562-499680aacfc6\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">将任何整数转换为带符号类型时，如果它可以在新类型中表示出来，则其值保持不变，否则它的值同具体的实现有关。</div></div></div></div><div data-block-id=\"f0778d95-955a-490c-aaec-865f9d448ec4\" class=\"notion-selectable notion-sub_header-block\" style=\"width: 100%; max-width: 608px; margin-top: 1.4em; margin-bottom: 1px; color: rgb(55, 53, 47);\"><div style=\"display: flex; width: 100%; font-weight: 600; font-size: 1.5em; line-height: 1.3; color: inherit; fill: inherit;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\"Heading 2\" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">3. 整数和浮点数</div></div></div><div data-block-id=\"8bb5932b-f543-42f5-9c1b-6bbb6d9b67bd\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">当把浮点类型的值转换为整型时，小数部分将被丢弃。如果结果值不能用整型表示，则其行为是未定义的。特别是，将负的浮点数转换为无符号整型的结果是没有定义的。</div></div></div></div><div data-block-id=\"345539c0-2157-4177-8759-3e2405ea6ce1\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">当把整型值转换为浮点类型时，如果该值在该浮点类型可表示的范围内但不能精确表示，则结果可能是下一个较高或较低的可表示值。如果该值超出可表示的范围，则其行为是为定义的。</div></div></div></div><div data-block-id=\"a61cadd9-3f78-4dc0-95ad-ad5dfaeebeef\" class=\"notion-selectable notion-sub_header-block\" style=\"width: 100%; max-width: 608px; margin-top: 1.4em; margin-bottom: 1px; color: rgb(55, 53, 47);\"><div style=\"display: flex; width: 100%; font-weight: 600; font-size: 1.5em; line-height: 1.3; color: inherit; fill: inherit;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\"Heading 2\" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">4. 浮点类型</div></div></div><div data-block-id=\"aff680f0-c89d-4685-9ef2-0b9ae139814b\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">将一个精度较低的浮点值转换为相同或更高精度的浮点类型时，它的值保持不变。将一个较高精度的浮点类型值转换为较低精度的浮点类型时，如果它的值在可表示范围内，则结果可能是下一个较高或较低的可表示值。如果结果在可表示范围之外，则其行为是为定义的。</div></div></div></div><div data-block-id=\"a514926a-4b3c-433b-9d51-838a463b3553\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">表达式中的float类型的操作数不会自动转换为double类型。一般来说，数学函数使用double类型的变量。使用float类型主要是为了在使用较大的数组时节省存储空间，有时也为了节省机器执行时间（double算术运算特别费时）。 ## 5. 算术类型转换 许多运算符都会以类似的方式在运算过程中引起转换，并产生结果类型。其效果是将所有操作数转换为同一公共类型，并以此作为结果的类型。这种方式的转换称为普通算术类型转换。</div></div></div></div><div data-block-id=\"253ec2c2-a201-4866-a320-7cf7a091da8a\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">首先，如果任何一个操作数为long double类型，则将另一个操作数转换为long double类型。</div></div></div></div><div data-block-id=\"cb5adcce-532a-4fcc-b831-07fb9129993e\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">否则，如果任何一个操作数为double类型，则将另一个操作数转换为double类型。</div></div></div></div><div data-block-id=\"d5e6ce71-77f0-4302-8e85-a2d3dc7aef04\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">否则，如果任何一个操作数为float类型，则将另一个操作数转换为float类型。</div></div></div></div><div data-block-id=\"92ac862d-afef-44fb-b5b1-a82351a4bbc6\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">否则，同时对两个操作数进行整型提升；然后，如果任何一个操作数为unsigned long int类型，则将另一个操作数转换为unsigned long int类型。</div></div></div></div><div data-block-id=\"8a379807-4cfc-434d-bd0b-ed3291464ae6\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">否则，如果一个操作数为long int类型且另一个操作数为unsigned int类型，则结果依赖于long int类型是否可以表示所有的unsigned int类型的值。如果可以，则将unsigned int类型的操作数转换为long int；如果不可以，则将两个操作数都转换为unsigned long int类型。</div></div></div></div><div data-block-id=\"d1bf9b36-73fd-4029-9616-2e1b3bbc4e80\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">否则，如果一个操作数为long int类型，则将另一个操作数转换为long int类型。</div></div></div></div><div data-block-id=\"88ddb874-8bb9-4672-a58d-e43f67dcf412\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">否则，如果任何一个操作数为unsigned int类型，则将另一个操作数转换为unsigned int类型。</div></div></div></div><div data-block-id=\"60dfc704-53b4-4853-9464-155bb731c262\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">否则，将两个操作数都转换为int类型。</div></div></div></div><div data-block-id=\"c73eb6a3-7494-4103-a802-6852e6f850df\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">用通俗语言，大致意思如下：</div></div></div></div><div data-block-id=\"8540e6de-bb2b-49ec-bff6-5c302048020e\" class=\"notion-selectable notion-quote-block\" style=\"width: 100%; max-width: 608px; margin-top: 4px; margin-bottom: 4px;\"><div style=\"font-size: 1.2em; padding: 3px 2px; color: inherit; fill: inherit; display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\"Empty quote\" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); border-left: 3px solid currentcolor; padding-left: 0.9em; padding-right: 0.9em;\">当执行算术运算时：操作数的类型如果不同，就会发生转换。数据类型一般朝着浮点精度更高，长度更长的方向转换，整型数如果转换为signed不会丢失信息，就转换为signed，否则转换为unsigned。</div></div></div><div data-block-id=\"d403b60d-723f-418f-885c-03af21d25737\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">例如，如果f为float类型，n为int类型，那么表达式</div></div></div></div><div data-block-id=\"e2369dc1-04c1-49f0-ac1e-4140c5aec3c9\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\"><span style=\"font-family:&quot;SFMono-Regular&quot;, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;line-height:normal;background:rgba(135,131,120,0.15);color:#EB5757;border-radius:3px;font-size:85%;padding:0.2em 0.4em\" data-token-index=\"0\">(n &gt; 0) ? f : n</span></div></div></div></div><div data-block-id=\"26f8596c-bca6-4d54-9de3-ad87de9fd664\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">是float类型，与n是否为正值无关。 ## 6. 参数传递中的类型转换 在参数传递过程中会发生隐式类型转换，这是需要注意的地方。由于函数的参数也是表达式，所以也会发生类型提升。如果使用了适当的函数原型，类型提升便不会发生，否则也会发生。在被调用函数的内部，提升后的参数被裁剪为原先声明的大小。</div></div></div></div><div data-block-id=\"ca54b227-306e-41b1-9bff-ea11a143570e\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">ANSI C的函数原型就是采取一种新的函数声明形式，把参数的类型也包含于声明之中。</div></div></div></div><div data-block-id=\"6284ec5d-7629-40ba-94a0-3ae7d5eba386\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">在C语言中，我们没有办法可以将一个数组作为参数直接传递。如果我们使用数组名作为参数，那么数组名会立刻被转换为指向数组第一个元素的指针。因此，将数组作为函数参数毫无意义。所以，C语言中会自动地将作为参数的数组声明转换为相应的指针声明。</div></div></div></div><div data-block-id=\"ba93f5ca-a00a-4aa9-9573-4a99d0011a93\" class=\"notion-selectable notion-code-block\" style=\"width: 100%; max-width: 608px; margin-top: 4px; margin-bottom: 4px;\"><div style=\"display: flex;\"><div style=\"flex-grow: 1; border-radius: 3px; text-align: left; position: relative; background: rgb(247, 246, 243); min-width: 0px; width: 100%;\"><div class=\"line-numbers notion-code-block\" style=\"display: flex; overflow-x: auto;\"><div contenteditable=\"false\" spellcheck=\"false\" autocorrect=\"off\" autocapitalize=\"off\" data-root=\"true\" style=\"flex-grow: 1; flex-shrink: 1; text-align: left; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 85%; tab-size: 2; padding: 30px 16px 30px 20px; min-height: 1em; color: rgb(55, 53, 47); white-space: pre;\">实参                                所匹配的形式参数\n数组的数组      char c[8][10];      char(*c)[10];    数组指针\n指针数组        char *c[15];        char **c;       指针的指针\n数组指针        char (*c)[64];      char (*c)[64];  不改变\n指针的指针      char **c;           char **c;       不改变<span>\n</span></div></div><div style=\"position: absolute; top: 0px; right: 0px; color: rgba(55, 53, 47, 0.6); display: flex; align-items: center; justify-content: flex-end; height: 30px;\"></div><div style=\"position: absolute; bottom: 0px; right: 0px; color: rgba(55, 53, 47, 0.6); display: flex; align-items: center; justify-content: flex-end; height: 30px;\"><div role=\"button\" aria-disabled=\"true\" tabindex=\"-1\" style=\"user-select: none; transition: background 120ms ease-in 0s; display: inline-flex; align-items: center; white-space: nowrap; height: 20px; border-radius: 3px; font-size: 12px; line-height: 1.2; padding-left: 5px; padding-right: 5px; color: rgba(55, 53, 47, 0.6); margin-right: 5px;\">Plain Text<svg viewBox=\"0 0 30 30\" class=\"chevronDown\" style=\"width: 10px; height: 100%; display: block; fill: rgba(55, 53, 47, 0.3); flex-shrink: 0; backface-visibility: hidden; margin-left: 4px;\"><polygon points=\"15,17.4 4.8,7 2,9.8 15,23 28,9.8 25.2,7 \"></polygon></svg></div></div></div></div></div><div data-block-id=\"c4f6517a-1a6f-445b-8a04-d6377b8fe56e\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">你之所以能在main()函数中看到<span style=\"font-family:&quot;SFMono-Regular&quot;, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;line-height:normal;background:rgba(135,131,120,0.15);color:#EB5757;border-radius:3px;font-size:85%;padding:0.2em 0.4em\" data-token-index=\"1\">char **argv</span>这样的参数，是因为argv是个指针数组(<span style=\"font-family:&quot;SFMono-Regular&quot;, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;line-height:normal;background:rgba(135,131,120,0.15);color:#EB5757;border-radius:3px;font-size:85%;padding:0.2em 0.4em\" data-token-index=\"3\">char *argv[]</span>)。这个表达式被编译器改写为指向数组第一个元素的指针，也就是一个指向指针的指针。 ## 7. 指针和整数 指针可以加上或减去一个整型表达式。在这种情况下，整型表达式的转换按照加法运算符的方法进行。</div></div></div></div><div data-block-id=\"65094538-957e-49c9-a2d1-965751a1fc92\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">两个指向同一数组中同一类型的对象的指针可以进行减法运算，其结果将被转换为整型；转换方式按照减法的方式进行。</div></div></div></div><div data-block-id=\"8aa44be5-b03f-4d6c-9962-5cd47b845397\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">值为0的整型常量表达式或强制转换为void *类型的表达式可以通过强制类型转换、赋值或比较操作转换为任意类型的指针。其结果将产生一个空指针，此空指针等于指向同一类型的另一空指针，但不等于任何指向函数或对象的指针。</div></div></div></div><div data-block-id=\"4cb16930-a8fd-4875-b7a4-cda56ba08e24\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">还允许进行指针相关的其他某些转换，但其结果依赖于具体实现。这些转换必须由一个显示的类型转换运算符或强制类型转换来指定。</div></div></div></div><div data-block-id=\"d7f0f4dc-0397-46ed-a30c-a2c0d655ae08\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">指针可以转换为整型，但此整型必须足够大;所要求的大小依赖于具体的实现。映射函数也依赖于具体的实现。</div></div></div></div><div data-block-id=\"f15fe9fc-79ff-451e-9b54-5b6605357321\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">整型对象可以显式地转换为指针。这种映射总是将一个足够宽的从指针转换来的整数转换为同一个指针，其他情况依赖于具体的实现。</div></div></div></div><div data-block-id=\"29739e61-f4ba-4138-af5d-9fd8b8b2cdb5\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">指向某一类型的指针可以转换为指向另一类型的指针，但是，如果该指针指向的对象不满足一定的存储对齐要求，则结果指针可能会导致地址异常。指向某对象的指针可以转换为一个指向具有更小或相同存储对齐限制的对象的指针，并可以保证原封不动地再转回来。“对齐”的概念依赖于具体的实现，但char类型的对象具有最小的对齐限制。指针也可以转换为void * 类型，并可原封不动地转换回来。</div></div></div></div><div data-block-id=\"110cef91-2e9a-45b9-97df-f131ea0df9a4\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">一个指针可以转换为同类型的另一个指针，但增加或删除了指针所指的对象类型的限定符除外。如果增加了限定符，则新指针与原指针等价，不同的是增加了限定符带来的限制。如果删除了限定符，则对底层对象的运算仍受实际声明中的限定符的限制。</div></div></div></div><div data-block-id=\"d86ad273-4450-414f-a9cf-6638b3ee8117\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">最后，指向一个函数的指针可以转换为指向另一个函数的指针。调用转换后指针所指的函数的结果依赖于具体的实现。但是，如果转换后的指针被重新转换为原来的类型，则结果与原来的指针一致。 ## 8. void void对象的值不能够以任何方式使用，也不能被显式或隐式地转换为任一非空类型。因为空（void）表达式表示一个不存在的值，这样的表达式只可以用在不需要值的地方，例如作为一个表达式语句或作为逗号运算符的做操作数。</div></div></div></div><div data-block-id=\"8a696b5e-ed38-4e51-bc31-2f75ecdb5153\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">可以通过强制类型转换将表达式转换为void类型。例如，在表达式语句中，一个空的强制类型转换将丢掉函数调用的返回值。 ## 9. 指向void的指针 指向任何对象的指针都可以转换为void * 类型，且不会丢失信息。如果将结果再转换为初始指针类型，则可以恢复初始指针。之前讨论过，执行指针的转换时，一般需要显式的强制转换，这里不同的是，指针可以被赋值为void * 类型的指针，也可以赋值给void * 类型的指针，并可与void * 类型的指针进行比较。 ## 10. 强制类型转换 之前都在说隐式类型转换，在任何表达式中都可以使用一个称为强制类型转换的一元运算符强制进行显式类型转换。</div></div></div></div><div data-block-id=\"4de9331e-6d64-42d6-b8e3-873e31fa95c7\" class=\"notion-selectable notion-text-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"color: inherit; fill: inherit;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\" \" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">(类型名) 表达式</div></div></div></div><div data-block-id=\"9ad23a32-c006-48df-b614-3cc88ed97161\" class=\"notion-selectable notion-sub_sub_header-block\" style=\"width: 100%; max-width: 608px; margin-top: 1em; margin-bottom: 1px; color: rgb(55, 53, 47);\"><div style=\"display: flex; width: 100%; font-weight: 600; font-size: 1.25em; line-height: 1.3; color: inherit; fill: inherit;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\"Heading 3\" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding: 3px 2px;\">参考文献</div></div></div><div data-block-id=\"8174fd10-6efe-4681-bbc2-a69634323549\" class=\"notion-selectable notion-bulleted_list-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"display: flex; align-items: flex-start; width: 100%; padding-left: 2px; color: inherit; fill: inherit;\"><div style=\"margin-right: 4px; width: 24px; display: flex; align-items: center; justify-content: center; flex-grow: 0; flex-shrink: 0; min-height: calc((1.5em + 3px) + 3px); padding-right: 2px;\"><div style=\"width: 6px; height: 6px; border-radius: 6px; background: currentcolor; margin-top: 0.1em;\"></div></div><div style=\"flex: 1 1 0px; min-width: 1px; display: flex; flex-direction: column;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\"List\" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding-top: 3px; padding-bottom: 3px; text-align: left;\">C程序设计语言</div></div></div></div></div><div data-block-id=\"13634543-4c74-4b09-9b95-05892bb13528\" class=\"notion-selectable notion-bulleted_list-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 1px;\"><div style=\"display: flex; align-items: flex-start; width: 100%; padding-left: 2px; color: inherit; fill: inherit;\"><div style=\"margin-right: 4px; width: 24px; display: flex; align-items: center; justify-content: center; flex-grow: 0; flex-shrink: 0; min-height: calc((1.5em + 3px) + 3px); padding-right: 2px;\"><div style=\"width: 6px; height: 6px; border-radius: 6px; background: currentcolor; margin-top: 0.1em;\"></div></div><div style=\"flex: 1 1 0px; min-width: 1px; display: flex; flex-direction: column;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\"List\" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding-top: 3px; padding-bottom: 3px; text-align: left;\">C专家编程</div></div></div></div></div><div data-block-id=\"4e23fd62-5a68-4658-a023-963c4e9a9a82\" class=\"notion-selectable notion-bulleted_list-block\" style=\"width: 100%; max-width: 608px; margin-top: 1px; margin-bottom: 0px;\"><div style=\"display: flex; align-items: flex-start; width: 100%; padding-left: 2px; color: inherit; fill: inherit;\"><div style=\"margin-right: 4px; width: 24px; display: flex; align-items: center; justify-content: center; flex-grow: 0; flex-shrink: 0; min-height: calc((1.5em + 3px) + 3px); padding-right: 2px;\"><div style=\"width: 6px; height: 6px; border-radius: 6px; background: currentcolor; margin-top: 0.1em;\"></div></div><div style=\"flex: 1 1 0px; min-width: 1px; display: flex; flex-direction: column;\"><div style=\"display: flex;\"><div contenteditable=\"false\" spellcheck=\"true\" placeholder=\"List\" data-root=\"true\" style=\"max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; caret-color: rgb(55, 53, 47); padding-top: 3px; padding-bottom: 3px; text-align: left;\">C缺陷与陷阱</div></div></div></div></div>","slug":"fd58d9d32292425eabb52ebec9a54f76","keywords":["博客"]}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"fd58d9d32292425eabb52ebec9a54f76"}}}