2024-07-30 07:13:16 +02:00
<!DOCTYPE html>
< html lang = "zh-CN" dir = "ltr" >
< head >
< meta charset = "utf-8" >
< title > 第一章:颇具争议的指针 | Hi, 终于等到你< / title >
< meta name = "description" content = "许大仙前端、Java、大数据、云原生" >
2024-08-09 04:52:37 +02:00
< meta name = "generator" content = "VitePress v1.3.2" >
2024-08-13 10:10:53 +02:00
< link rel = "preload stylesheet" href = "/c/assets/style.vKaqWsDC.css" as = "style" >
2024-07-30 07:13:16 +02:00
2024-08-15 03:35:38 +02:00
< script type = "module" src = "/c/assets/app.Cr6JV6rH.js" > < / script >
2024-07-30 07:13:16 +02:00
< link rel = "preload" href = "/c/assets/inter-roman-latin.Di8DUHzh.woff2" as = "font" type = "font/woff2" crossorigin = "" >
2024-08-15 03:35:38 +02:00
< link rel = "modulepreload" href = "/c/assets/chunks/theme.DTfBiwvN.js" >
2024-08-09 04:52:37 +02:00
< link rel = "modulepreload" href = "/c/assets/chunks/framework.CZRoMP2i.js" >
2024-08-13 04:40:27 +02:00
< link rel = "modulepreload" href = "/c/assets/notes_01_c-basic_06_xdx_index.md.Crg8JAyH.lean.js" >
2024-07-30 07:13:16 +02:00
< link rel = "shortcut icon" href = "/c//logo.svg" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,shrink-to-fit=no" >
< meta name = "keywords" content = "许大仙,许大仙的博客" >
< script id = "check-dark-mode" > ( ( ) => { const e = localStorage . getItem ( "vitepress-theme-appearance" ) || "auto" , a = window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches ; ( ! e || e === "auto" ? a : e === "dark" ) && document . documentElement . classList . add ( "dark" ) } ) ( ) ; < / script >
< script id = "check-mac-os" > document . documentElement . classList . toggle ( "mac" , /Mac|iPhone|iPod|iPad/i . test ( navigator . platform ) ) ; < / script >
< / head >
< body >
2024-08-13 04:40:27 +02:00
< div id = "app" > < div class = "Layout" data-v-5d98c3a5 > <!-- [ --> <!-- ] --> <!-- [ --> < span tabindex = "-1" data-v-0f60ec36 > < / span > < a href = "#VPContent" class = "VPSkipLink visually-hidden" data-v-0f60ec36 > Skip to content < / a > <!-- ] --> <!-- --> < header class = "VPNav" data-v-5d98c3a5 data-v-ae24b3ad > < div class = "VPNavBar has-sidebar top" data-v-ae24b3ad data-v-6aa21345 > < div class = "wrapper" data-v-6aa21345 > < div class = "container" data-v-6aa21345 > < div class = "title" data-v-6aa21345 > < div class = "VPNavBarTitle has-sidebar" data-v-6aa21345 data-v-ab179fa1 > < a class = "title" href = "/c/" data-v-ab179fa1 > <!-- [ --> <!-- ] --> <!-- [ --> < img class = "VPImage logo" src = "/c/logo.svg" alt data-v-8426fc1a > <!-- ] --> < span data-v-ab179fa1 > 许大仙< / span > <!-- [ --> <!-- ] --> < / a > < / div > < / div > < div class = "content" data-v-6aa21345 > < div class = "content-body" data-v-6aa21345 > <!-- [ --> <!-- ] --> < div class = "VPNavBarSearch search" data-v-6aa21345 > <!-- [ --> <!-- --> < div id = "local-search" > < button type = "button" class = "DocSearch DocSearch-Button" aria-label = "Search" > < span class = "DocSearch-Button-Container" > < span class = "vp-icon DocSearch-Search-Icon" > < / span > < span class = "DocSearch-Button-Placeholder" > Search< / span > < / span > < span class = "DocSearch-Button-Keys" > < kbd class = "DocSearch-Button-Key" > < / kbd > < kbd class = "DocSearch-Button-Key" > K< / kbd > < / span > < / button > < / div > <!-- ] --> < / div > < nav aria-labelledby = "main-nav-aria-label" class = "VPNavBarMenu menu" data-v-6aa21345 data-v-dc692963 > < span id = "main-nav-aria-label" class = "visually-hidden" data-v-dc692963 > Main Navigation < / span > <!-- [ --> <!-- [ --> < a class = "VPLink link vp-external-link-icon VPNavBarMenuLink" href = "https://aexiar.github.io/" target = "_blank" rel = "noreferrer" tabindex = "0" data-v-dc692963 data-v-9c663999 > <!-- [ --> < span data-v-9c663999 > 首页< / span > <!-- ] --> < / a > <!-- ] --> <!-- [ --> < a class = "VPLink link vp-external-link-icon VPNavBarMenuLink" href = "https://aexiar.github.io/coa6/notes/" target = "_blank" rel = "noreferrer" tabindex = "0" data-v-dc692963 data-v-9c663999 > <!-- [ --> < span data-v-9c663999 > 计组6件套< / span > <!-- ] --> < / a > <!-- ] --> <!-- [ --> < div class = "VPFlyout VPNavBarMenuGroup" data-v-dc692963 data-v-b6c34ac9 > < button type = "button" class = "button" aria-haspopup = "true" aria-expanded = "false" data-v-b6c34ac9 > < span class = "text" data-v-b6c34ac9 > <!-- --> < span data-v-b6c34ac9 > 编程语言< / span > < span class = "vpi-chevron-down text-icon" data-v-b6c34ac9 > < / span > < / span > < / button > < div class = "menu" data-v-b6c34ac9 > < div class = "VPMenu" data-v-b6c34ac9 data-v-b98bc113 > < div class = "items" data-v-b98bc113 > <!-- [ --> <!-- [ --> < div class = "VPMenuLink" data-v-b98bc113 data-v-43f1e123 > < a class = "VPLink link" href = "/c/notes/" data-v-43f1e123 > <!-- [ --> c/c++<!-- ] --> < / a > < / div > <!-- ] --> <!-- [ --> < div class = "VPMenuLink" data-v-b98bc113 data-v-43f1e123 > < a class = "VPLink link vp-external-link-icon" href = "https://aexiar.github.io/web-design/notes/" target = "_blank" rel = "noreferrer" data-v-43f1e123 > <!-- [ --> 前端<!-- ] --> < / a > < / div > <!-- ] --> <!-- [ --> < div class = "VPMenuLink" data-v-b98bc113 data-v-43f1e123 > < a class = "VPLink link vp-external-link-icon" href = "https://aexiar.github.io/java/notes/" target = "_blank" rel = "noreferrer" data-v-43f1e123 > <!-- [ --> Java<!-- ] --> < / a > < / div > <!-- ] --> <!-- [ --> < div class = "VPMenuLink" data-v-b98bc113 data-v-43f1e123 > < a class = "VPLink link vp-external-link-icon" href = "https://aexiar.github.io/big-data/notes/" target = "_blank" rel = "noreferrer" data-v-43f1e123 > <!-- [ --> 大数据<!-- ] --> < / a > < / div > <!-- ] --> <!-- [ --> < div class = "VPMenuLink" data-v-b98bc113 data-v-43f1e123 > < a class = "VPLink link vp-external-link-icon" href = "https://aexiar.github.io/linux/notes/" target = "_blank" rel = "noreferrer" data-v-43f1e123 > <!-- [ --> 云原生<!-- ] --> < / a > < / div > <!-- ] --> <!-- [ --> < div class = "VPMenuLink" data-v-b98bc113 data-v-43f1e123 > < a class = "VPLink link vp-external-link-icon" href = "https://aexiar.github.io/go/notes/" target = "_blank" rel = "noreferrer" data-v-43f1e123 > <!-- [ --> Go<!-- ] --> < / a > < / div > <!-- ] --> <!-- [ --> < div class = "VPMenuLink" data-v-b98bc113 data-v-43f1e123 > < a class = "VPLink link vp-external-link-icon" href = "https://aexiar.github.io/python/notes/" target = "_blank" rel = "noreferrer" data-v-43f1e123 > <!-- [ --> Python<!-- ] - -
2024-08-08 04:25:43 +02:00
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义变量,即:开辟一块内存空间,并将初始化值存储进去< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 访问变量,即:访问变量在内存中对应的数据< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " num = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , num);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 给变量赋值,即:给变量在内存中占据的内存空间存储数据< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 100< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 访问变量,即:访问变量在内存中对应的数据< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " num = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , num);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-08-08 04:41:18 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < / div > < / div > < ul > < li > 上述的这种方式也称为< code > 直接访问< / code > ;当然,既然有< code > 直接访问< / code > 的方式,必然有< code > 间接访问< / code > 的方式,如:< code > 指针< / code > 。< / li > < / ul > < div class = "important custom-block github-alert" > < p class = "custom-block-title" > IMPORTANT< / p > < p > < / p > < ul > < li > ① 我们通过< code > 变量名(普通变量)< / code > 访问内存中变量存储的数据,之所以称为< code > 直接访问< / code > 的方式,是因为对于我们写程序而言,我们无需关心如何根据内存地址去获取内存中对应的数据,也无需关系如何根据内存地址将数据存储到对应的内存空间,这些操作步骤都是< code > 编译器< / code > 帮助我们在底层自动完成的(自动化)。< / li > < li > ② 但是,我们也可以通过< code > 内存地址< / code > 去操作内存中对应的数据(手动化),这种方式就称为< code > 间接访问< / code > 的方式了,相对于< code > 直接访问< / code > 方式来说,要< code > 理解< / code > 的< code > 概念< / code > 和< code > 操作< / code > 的< code > 步骤< / code > 和之前< code > 直接访问< / code > 的方式相比,要复杂和麻烦很多,但是效率高。< / li > < / ul > < / div > < h2 id = "_3-2-内存地址和指针" tabindex = "-1" > 3.2 内存地址和指针 < a class = "header-anchor" href = "#_3-2-内存地址和指针" aria-label = "Permalink to "3.2 内存地址和指针"" > < / a > < / h2 > < ul > < li > 其实,在之前《数组》中,我们就已经讲解了< code > 内存地址< / code > 的概念了,即:操作系统为了更快的去管理内存中的数据,会将< code > 内存条< / code > 按照< code > 字节< / code > 划分为一个个的< code > 单元格< / code > ,并为每个独立的小的< code > 单元格< / code > ,分配< code > 唯一的编号< / code > ,即:< code > 内存地址< / code > ,如下所示:< / li > < / ul > < p > < img src = "/c/assets/4.DqDR6Thp.svg" alt = "" > < / p > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 有了内存地址,就能加快数据的存取速度,可以类比生活中的< code > 字典< / code > ,即:< / p > < ul > < li > ① 内存地址是计算机中用于标识内存中某个特定位置的数值。< / li > < li > ② 每个内存单元都有一个唯一的地址,这些地址可以用于访问和操作存储在内存中的数据。< / li > < / ul > < / div > < ul > < li > 对于之前的代码,如下所示:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
2024-08-08 04:25:43 +02:00
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义变量,即:开辟一块内存空间,并将初始化值存储进去< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-08-11 06:51:13 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < / div > < / div > < ul > < li > 虽然,之前我们在程序中都是通过< code > 变量名(普通变量)< / code > 直接操作内存中的存储单元;但是,编译器底层还是会通过< code > 内存地址< / code > 来找到所需要的存储单元,如下所示:< / li > < / ul > < p > < img src = "/c/assets/5.BzSkS-4w.svg" alt = "" > < / p > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 通过< code > 内存地址< / code > 找到所需要的< code > 存储单元< / code > ,即:内存地址指向该存储单元。此时,就可以将< code > 内存地址< / code > 形象化的描述为< code > 指针👉< / code > ,那么:< / p > < ul > < li > ① < code > 变量< / code > :命名的内存空间,用于存放各种类型的数据。< / li > < li > ② < code > 变量名< / code > :变量名是给内存空间取一个容易记忆的名字,方便我们编写程序。< / li > < li > ③ < code > 变量值< / code > :变量所对应的内存中的存储单元中存放的数据值。< / li > < li > ④ < code > 变量的地址< / code > :变量所对应的内存中的存储单元的内存地址(首地址),也可以称为< code > 指针< / code > 。< / li > < / ul > < p > 总结:内存地址 = 指针。< / p > < / div > < ul > < li > < code > 普通变量< / code > 所对应的内存空间< code > 存储< / code > 的是< code > 普通的值< / code > ,如:整数、小数、字符等;< code > 指针变量< / code > 所对应的内存空间< code > 存储< / code > 的是另外一个变量的< code > 地址(指针)< / code > ,如下所示:< / li > < / ul > < p > < img src = "/c/assets/6.BPY9ZGed.svg" alt = "" > < / p > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 有的时候,为了方便阐述,我们会将< code > 指针变量< / code > 称为< code > 指针< / code > 。但是,需要记住的是:< / p > < ul > < li > 指针 = 内存地址。< / li > < li > 指针变量 = 变量中保存的是另一个变量的地址。< / li > < / ul > < p > 下文中提及的< code > 指针< / code > 都是< code > 指针变量< / code > ,不再阐述!!!< / p > < / div > < div class = "important custom-block github-alert" > < p class = "custom-block-title" > IMPORTANT< / p > < p > < / p > < p > 如果你观察仔细的话,你可能会发现< code > 指针变量< / code > 和< code > 普通变量< / code > 在内存中占据的存储空间是不一样的,那么到底是什么原因造成这样的结果?< / p > < / div > < h2 id = "_3-3-指针变量的定义" tabindex = "-1" > 3.3 指针变量的定义 < a class = "header-anchor" href = "#_3-3-指针变量的定义" aria-label = "Permalink to "3.3 指针变量的定义"" > < / a > < / h2 > < h2 id = "_3-4-指针的作用" tabindex = "-1" > 3.4 指针的作用 < a class = "header-anchor" href = "#_3-4-指针的作用" aria-label = "Permalink to "3.4 指针的作用"" > < / a > < / h2 > < ul > < li > 查询数据。< / li > < li > 存储数据。< / li > < li > 参数传递。< / li > < li > 内存管理。< / li > < / ul > < p > 在 Java 中, 引用数据类型的向上类型转换( upcasting) 和向下类型转换( downcasting) 是面向对象编程中常见的操作。这些转换是 Java 继承体系和多态性的重要部分。我们先分别介绍向上类型转换和向下类型转换,然后讨论它们在 C 语言中指针的类似操作。< / p > < h3 id = "向上类型转换-upcasting" tabindex = "-1" > 向上类型转换( Upcasting) < a class = "header-anchor" href = "#向上类型转换-upcasting" aria-label = "Permalink to "向上类型转换( Upcasting) "" > < / a > < / h3 > < p > 向上类型转换是将一个子类对象引用转换为父类对象引用。由于子类继承了父类的所有方法和属性,子类对象也包含父类对象的所有部分,因此这种转换是安全且隐式的。< / p > < p > < s
2024-08-08 11:22:11 +02:00
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > void< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > makeSound< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > System.out.< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > println< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " Animal sound" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > class< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > Dog< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > extends< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > Animal< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > void< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > makeSound< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > System.out.< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > println< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " Bark" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > public< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > class< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > Main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > public< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > static< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > void< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > String< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [] < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > args< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > Dog dog < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > new< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > Dog< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ();< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > Animal animal < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > dog; < / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // Upcasting, 隐式转换< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > animal.< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > makeSound< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (); < / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 输出: Bark< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < / div > < / div > < p > 在这个例子中,< code > Dog< / code > 类型的对象 < code > dog< / code > 被转换为 < code > Animal< / code > 类型。尽管 < code > animal< / code > 引用的实际对象是 < code > Dog< / code > ,但在编译时,< code > animal< / code > 被视为 < code > Animal< / code > 类型。< / p > < h3 id = "向下类型转换-downcasting" tabindex = "-1" > 向下类型转换( Downcasting) < a class = "header-anchor" href = "#向下类型转换-downcasting" aria-label = "Permalink to "向下类型转换( Downcasting) "" > < / a > < / h3 > < p > 向下类型转换是将一个父类对象引用转换为子类对象引用。由于父类对象不一定具有子类的所有方法和属性,因此这种转换需要显式进行,并且在运行时进行类型检查(使用 < code > instanceof< / code > 关键字来确保安全)。< / p > < p > < strong > 例子:< / strong > < / p > < div class = "language-java vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > java< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > public< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > class< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > Main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > public< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > static< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > void< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > String< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [] < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > args< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > Animal animal < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > new< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > Dog< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (); < / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // Upcasting< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > if< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (animal < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > instanceof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > Dog) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > Dog dog < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (Dog) animal; < / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // Downcasting, 显式转换< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > dog.< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > makeSound< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (); < / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 输出: Bark< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < / div > < / div > < p > 在这个例子中,< code > animal< / code > 引用的对象实际是 < code > Dog< / code > 类型,在向下转换之前使用 < code > instanceof< / code > 检查以确保安全。< / p > < h3 id = "区别" tabindex = "-1" > 区别 < a class = "header-anchor" href = "#区别" aria-label = "Permalink to "区别"" > < / a > < / h3 > < ul > < li > < strong > 向上类型转换< / strong > :隐式的,安全的,因为子类是父类的扩展。< / li > < li > < strong > 向下类型转换< / strong > :显式的,可能不安全,需要运行时检查,因为父类不一定具有子类的特性。< / li > < / ul > < h3 id = "c-语言中的指针转换" tabindex = "-1" > C 语言中的指针转换 < a class = "header-anchor" href = "#c-语言中的指针转换" aria-label = "Permalink to "C 语言中的指针转换"" > < / a > < / h3 > < p > 在 C 语言中,指针的转换类似于引用类型的转换,但由于 C 语言没有继承和多态的概念,其转换更多是基于内存布局。< / p > < p > < strong > 例子:< / strong > < / p > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > void< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printInt< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > void*< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > ptr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > *< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int*< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > )ptr);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
2024-08-01 00:43:29 +02:00
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
2024-08-08 11:22:11 +02:00
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > x < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > void*< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > voidPtr < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > x;< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // Upcasting, 隐式转换< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printInt< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (voidPtr);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 输出: 10< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > < / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int*< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > intPtr < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int*< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > )voidPtr;< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // Downcasting, 显式转换< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > *< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > intPtr);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 输出: 10< / span > < / span >
2024-08-01 00:43:29 +02:00
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-08-08 11:22:11 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < / div > < / div > < p > 在这个例子中,< code > void*< / code > 是通用指针类型,可以指向任何类型的数据。将 < code > int*< / code > 转换为 < code > void*< / code > 是隐式的,而将 < code > void*< / code > 转换为 < code > int*< / code > 是显式的。由于 C 没有类型检查,因此这种转换需要程序员自己确保安全。< / p > < p > 总结:< / p > < ul > < li > Java 中的向上类型转换和向下类型转换是为了支持多态性和继承,向上转换是安全的,向下转换需要显式进行并且进行运行时检查。< / li > < li > C 语言中的指针转换没有多态性和继承的概念,但有类似的指针类型转换操作,程序员需要确保转换的安全性。< / li > < / ul > < h1 id = "第四章-指针的运算-⭐" tabindex = "-1" > 第四章:指针的运算(⭐) < a class = "header-anchor" href = "#第四章-指针的运算-⭐" aria-label = "Permalink to "第四章:指针的运算(⭐)"" > < / a > < / h1 > < h2 id = "_4-1-概述" tabindex = "-1" > 4.1 概述 < a class = "header-anchor" href = "#_4-1-概述" aria-label = "Permalink to "4.1 概述"" > < / a > < / h2 > < h2 id = "_4-2-总结" tabindex = "-1" > 4.2 总结 < a class = "header-anchor" href = "#_4-2-总结" aria-label = "Permalink to "4.2 总结"" > < / a > < / h2 > < ul > < li > < p > 在 C 语言中,< code > 普通变量< / code > 是直接存储< code > 数据< / code > 的< code > 变量< / code > 。对于普通变量,支持的操作包括:< / p > < ul > < li > ① < strong > 赋值操作< / strong > :给变量赋值,如:< code > int a = 5< / code > 。< / li > < li > ② < strong > 算术运算< / strong > :可以对数值类型的普通变量进行加、减、乘、除等运算,如:< code > a + b< / code > 、< code > a - b< / code > 、< code > a * b< / code > 、< code > a / b< / code > < / li > < li > ③ < strong > 关系运算< / strong > :可以进行比较运算(大于、小于、等于等),如: < code > a > b< / code > 、< code > a == b< / code > 。< / li > < li > ④ < strong > 逻辑运算< / strong > :对布尔类型的值进行与、或、非运算,如: < code > a & & b< / code > 、< code > a || b< / code > 、< code > !a< / code > 。< / li > < li > ⑤ < strong > 位运算< / strong > :对整数类型的值进行位操作(与、或、异或、取反、左移、右移等),如: < code > a & b< / code > 、< code > a | b< / code > 、< code > a ^ b< / code > 、< code > ~a< / code > 、< code > a < < 2< / code > 、< code > a > > 2< / code > 。< / li > < li > ⑥ < strong > 自增自减运算< / strong > : < code > a++< / code > 、< code > --a< / code > 等。< / li > < / ul > < / li > < li > < p > 在 C 语言中,< code > 指针变量< / code > 存储的是< code > 另一个变量< / code > 的< code > 地址< / code > 。对于指针变量,支持的操作包括:< / p > < ul > < li > ① < strong > 赋值操作< / strong > :可以将一个地址赋值给指针,如: < code > int *p = & a;< / code > 。< / li > < li > ② < strong > 解引用操作< / strong > :通过指针访问它指向的变量,如: < code > *p = 10;< / code > 修改指向变量的值。< / li > < li > ③ < strong > 指针运算< / strong > : < ul > < li > < strong > 指针和整数值的加减运算< / strong > :指针可以进行整数的加减运算,用于访问数组或结构体成员,如: < code > p + 1< / code > 。< / li > < li > < strong > 指针的自增和自减运算< / strong > :指的是内存地址的向前或向后移动,如:< code > p++< / code > 、< code > p--< / code > 。< / li > < li > < strong > 指针间的相减运算< / strong > :两个指向同一数组的指针相减可以得到它们之间的元素个数,如
2024-07-31 03:20:24 +02:00
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > *< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ptr < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr;< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 这句话是合法的, ptr现在指向arr[0]< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , arr);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印数组名,会打印数组首地址< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印第一个元素的地址< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < / div > < / div > < / li > < li > < p > < strong > 数组名是常量指针< / strong > : 数组名是一个常量指针,不能改变它指向的位置,而指针变量可以改变它指向的位置。< / p > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 5< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > *< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ptr < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr;< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 合法, ptr指向arr[0]< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ptr< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 合法, ptr现在指向arr[1]< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr++; // 非法,编译错误,因为数组名是常量,不能改变< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < / div > < / div > < / li > < li > < p > < strong > sizeof运算符的结果不同< / strong > : 使用< code > sizeof< / code > 运算符对数组名和指针变量会得到不同的结果,数组名会返回整个数组的大小,而指针变量会返回指针本身的大小。< / p > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 5< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > *< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ptr < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr;< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " sizeof(arr) = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %lu\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (arr));< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 返回数组的大小, 5 * sizeof(int)< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " sizeof(ptr) = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %lu\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (ptr));< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 返回指针的大小, 通常是4或8字节< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < / div > < / div > < / li > < li > < p > < strong > 地址运算符的结果不同< / strong > : 使用地址运算符< code > & < / code > 对数组名和指针变量会得到不同的结果,对数组名使用< code > & < / code > 会返回数组的地址,而对指针变量使用< code > & < / code > 会返回指针变量本身的地址。< / p > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 5< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > *< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ptr < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr;< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " Address of array: < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 返回整个数组的地址< / span > < / span >
2024-08-13 04:40:27 +02:00
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " Address of pointer: < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > ptr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 返回指针变量ptr的地址< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < / div > < / div > < / li > < / ol > < p > 综上所述,通过这些示例和解释,可以看出数组名虽然在某些场合下可以像指针一样使用,但它并不是一个真正的指针变量,而是一个常量,表示数组的首地址。< / p > < / div > < / div > < / main > < footer class = "VPDocFooter" data-v-39a288b8 data-v-e257564d > <!-- [ --> <!-- ] --> < div class = "edit-info" data-v-e257564d > <!-- --> < div class = "last-updated" data-v-e257564d > < p class = "VPLastUpdated" data-v-e257564d data-v-e98dd255 > 上次更新: < time datetime = "2024-08-13T02:39:52.000Z" data-v-e98dd255 > < / time > < / p > < / div > < / div > < nav class = "prev-next" aria-labelledby = "doc-footer-aria-label" data-v-e257564d > < span class = "visually-hidden" id = "doc-footer-aria-label" data-v-e257564d > Pager< / span > < div class = "pager" data-v-e257564d > < a class = "VPLink link pager-link prev" href = "/c/notes/01_c-basic/05_xdx/" data-v-e257564d > <!-- [ --> < span class = "desc" data-v-e257564d > 上一篇< / span > < span class = "title" data-v-e257564d > 数组< / span > <!-- ] --> < / a > < / div > < div class = "pager" data-v-e257564d > < a class = "VPLink link pager-link next" href = "/c/notes/01_c-basic/07_xdx/" data-v-e257564d > <!-- [ --> < span class = "desc" data-v-e257564d > 下一篇< / span > < span class = "title" data-v-e257564d > 函数< / span > <!-- ] --> < / a > < / div > < / nav > < / footer > <!-- [ --> <!-- ] --> < / div > < / div > < / div > <!-- [ --> <!-- ] --> < / div > < / div > < footer class = "VPFooter has-sidebar" data-v-5d98c3a5 data-v-e315a0ad > < div class = "container" data-v-e315a0ad > < p class = "message" data-v-e315a0ad > Released under the MIT License.< / p > < p class = "copyright" data-v-e315a0ad > Copyright © 2024 许大仙< / p > < / div > < / footer > <!-- [ --> <!-- ] --> < / div > < / div >
2024-08-15 03:35:38 +02:00
< script > w i n d o w . _ _ V P _ H A S H _ M A P _ _ = J S O N . p a r s e ( " { \ " i n d e x . m d \ " : \ " n 0 W - F d - u \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 0 _ x d x _ i n d e x . m d \ " : \ " L i P d c Y P t \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 1 _ x d x _ i n d e x . m d \ " : \ " B X w 7 f 8 5 o \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 2 _ x d x _ i n d e x . m d \ " : \ " 0 G H s M M a 0 \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 3 _ x d x _ i n d e x . m d \ " : \ " C w Y u K 6 w B \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 4 _ x d x _ i n d e x . m d \ " : \ " D k X f R s s o \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 5 _ x d x _ i n d e x . m d \ " : \ " 7 - U g _ F A C \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 6 _ x d x _ i n d e x . m d \ " : \ " C r g 8 J A y H \ " , \ " n o t e s _ 0 4 _ l i n u x _ 0 1 _ x d x _ i n d e x . m d \ " : \ " D 7 - U l H S H \ " , \ " n o t e s _ i n d e x . m d \ " : \ " b g N n s 6 d 1 \ " } " ) ; w i n d o w . _ _ V P _ S I T E _ D A T A _ _ = J S O N . p a r s e ( " { \ " l a n g \ " : \ " z h - C N \ " , \ " d i r \ " : \ " l t r \ " , \ " t i t l e \ " : \ " 许 大 仙 \ " , \ " t i t l e T e m p l a t e \ " : \ " H i , 终 于 等 到 你 \ " , \ " d e s c r i p t i o n \ " : \ " 许 大 仙 前 端 、 J a v a 、 大 数 据 、 云 原 生 \ " , \ " b a s e \ " : \ " / c / \ " , \ " h e a d \ " : [ ] , \ " r o u t e r \ " : { \ " p r e f e t c h L i n k s \ " : t r u e } , \ " a p p e a r a n c e \ " : t r u e , \ " t h e m e C o n f i g \ " : { \ " l a s t U p d a t e d T e x t \ " : \ " 上 次 更 新 \ " , \ " r e t u r n T o T o p L a b e l \ " : \ " 返 回 顶 部 \ " , \ " s e a r c h \ " : { \ " p r o v i d e r \ " : \ " l o c a l \ " } , \ " l o g o \ " : \ " / l o g o . s v g \ " , \ " n a v \ " : [ { \ " t e x t \ " : \ " 首 页 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / \ " } , { \ " t e x t \ " : \ " 计 组 6 件 套 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / c o a 6 / n o t e s / \ " } , { \ " t e x t \ " : \ " 编 程 语 言 \ " , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " c / c + + \ " , \ " l i n k \ " : \ " / n o t e s / \ " } , { \ " t e x t \ " : \ " 前 端 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / w e b - d e s i g n / n o t e s / \ " } , { \ " t e x t \ " : \ " J a v a \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / j a v a / n o t e s / \ " } , { \ " t e x t \ " : \ " 大 数 据 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / b i g - d a t a / n o t e s / \ " } , { \ " t e x t \ " : \ " 云 原 生 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / l i n u x / n o t e s / \ " } , { \ " t e x t \ " : \ " G o \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / g o / n o t e s / \ " } , { \ " t e x t \ " : \ " P y t h o n \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / p y t h o n / n o t e s / \ " } ] } , { \ " t e x t \ " : \ " 开 源 软 件 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / o p e n - s o f t w a r e / n o t e s / \ " } ] , \ " s i d e b a r \ " : { \ " / n o t e s / \ " : [ { \ " t e x t \ " : \ " C 语 言 基 础 \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " 编 程 基 础 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 0 _ x d x / \ " } , { \ " t e x t \ " : \ " C 语 言 入 门 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 1 _ x d x / \ " } , { \ " t e x t \ " : \ " 变 量 和 进 制 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 2 _ x d x / \ " } , { \ " t e x t \ " : \ " 数 据 类 型 和 运 算 符 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 3 _ x d x / \ " } , { \ " t e x t \ " : \ " 流 程 控 制 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 4 _ x d x / \ " } , { \ " t e x t \ " : \ " 数 组 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 5 _ x d x / \ " } , { \ " t e x t \ " : \ " 指 针 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 6 _ x d x / \ " } , { \ " t e x t \ " : \ " 函 数 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 7 _ x d x / \ " } , { \ " t e x t \ " : \ " 预 处 理 器 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 8 _ x d x / \ " } , { \ " t e x t \ " : \ " 自 定 义 数 据 类 型 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 9 _ x d x / \ " } , { \ " t e x t \ " : \ " 内 存 管 理 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 1 0 _ x d x / \ " } , { \ " t e x t \ " : \ " 文 件 操 作 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 1 1 _ x d x / \ " } , { \ " t e x t \ " : \ " 调 试 工 具 和 调 试 技 巧 ( g d b 和 m a k e ) \ " , \ " l i n k \ " : \ " / n o t e s / 0 2 _ c - b a s i c / 1 3 _ x d x / \ " } , { \ " t e x t \ " : \ " 常 用 库 函 数 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 1 3 _ x d x / \ " } ] } , { \ " t e x t \ " : \ " C 语 言 高 级 \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " 变 量 和 内 存 分 布 \ " , \ " l i n k \ " : \ " / n o t e s / 0 2 _ c - a d v a n c e / 0 1 _ x d x / \ " } , { \ " t e x t \ " : \ " 指 针 强 化 \ " , \ " l i n k \ " : \ " / n o t e s / 0 2 _ c - a d v a n c e / 0 2 _ x d x / \ " } , { \ " t e x t \ " : \ " 结 构 体 和 文 件 的 高 级 用 法 \ " , \ " l i n k \ " : \ " / n o t e s / 0 2 _ c - a d v a n c e / 0 3 _ x d x / \ " } , { \ " t e x t \ " : \ " 链 表 和 回 调 函 数 \ " , \ " l i n k \ " : \ " / n o t e s / 0 2 _ c - a d v a n c e / 0 4 _ x d x / \ " } , { \ " t e x t \ " : \ " 编 译 过 程 和 面 向 接 口 \ " , \ " l i n k \ " : \ " / n o t e s / 0 2 _ c - a d v a n c e / 0 5 _ x d x / \ " } ] } , { \ " t e x t \ " : \ " 项 目 构 建 工 具 \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " m e s o n \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " m e s o n 入 门 \ " , \ " l i n k \ " : \ " / n o t e s / 0 3 _ b u i l d / 0 1 _ x d x / \ " } ] } , { \ " t e x t \ " : \ " C m a k e \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " C m a k e 入 门 \ " , \ " l i n k \ " : \ " / n o t e s / 0 3 _ b u i l d / 0 1 _ x d x / \ " } ] } , { \ " t e x t \ " : \ " C o n a n \ " , \ " l i n k \ " : \ " / n o t e s / 0 3 _ b u i l d / 0 1 _ $ { c o m m o n D i r e c t o r y N a m e } / \ " } , { \ " t e x t \ " : \ " G r a d l e \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " G r a d l e 入 门 \ " , \ " l i n k \ " : \ " / n o t e s / 0 3 _ b u i l d / 0 1 _ x d x / \ " } ] } ] } , { \ " t e x t \ " : \ " L i n u x \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " L i n u x 初 识 和 安 装 \ " , \ " l i n k \ " : \ " / n o t e s / 0 4 _ l i n u x / 0 1 _ x d x / \ " } ] } , { \ " t e x t \ " : \ " C + + 基 础 \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " C + + 入 门 \ " , \ " l i n k \ " : \ " / n o t e s / 0 5 _ c p p / 0 1 _ x d x / \ " } , { \ " t e x t \ " : \ " 变 量 、 数 据 类 型 、 运 算 符 \ " , \ " l i n k \ " : \ " / n o t e s / 0 5 _ c p p / 0 2 _ x d x / \ " } , { \ " t e x t \ " : \ " 流 程 控 制 \ " , \ " l i n k \ "
2024-07-30 07:13:16 +02:00
< / body >
< / html >