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、大数据、云原生" >
< meta name = "generator" content = "VitePress v1.3.1" >
< link rel = "preload stylesheet" href = "/c/assets/style.DF3ihcHP.css" as = "style" >
2024-08-07 09:20:20 +02:00
< script type = "module" src = "/c/assets/app.DNoYS9Zm.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 = "" >
< link rel = "modulepreload" href = "/c/assets/chunks/framework.CZKtKhAm.js" >
2024-08-07 09:20:20 +02:00
< link rel = "modulepreload" href = "/c/assets/chunks/theme.BckjEKqh.js" >
2024-08-01 09:09:59 +02:00
< link rel = "modulepreload" href = "/c/assets/notes_01_c-basic_06_xdx_index.md.BBHptzz_.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-04 10:07:51 +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-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 >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > a < / 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;" > int< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > *< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > p < / 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;" > a;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > std< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ::cout < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " a: " < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > a < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " , p: " < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > p < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < < / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > std< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ::endl;< / span > < / 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 >
< 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 > < / div > < / div > < p > 在 CLion 中设置断点并开始调试,程序将在 < code > std::cout< / code > 行暂停。此时在调试控制台中输入以下命令:< / p > < ul > < li > < code > print a< / code > :输出变量 < code > a< / code > 的值,应该是 < code > 10< / code > 。< / li > < li > < code > print p< / code > :输出指针变量 < code > p< / code > 的值,即 < code > a< / code > 的地址。< / li > < li > < code > print *p< / code > :输出指针 < code > p< / code > 指向的值,即 < code > 10< / code > 。< / li > < / ul > < p > 通过这些命令,你可以看到指针变量 < code > p< / code > 实际上存储的是一个地址,而普通变量 < code > a< / code > 存储的是一个整数值。< / p > < h4 id = "使用反汇编" tabindex = "-1" > 使用反汇编 < a class = "header-anchor" href = "#使用反汇编" aria-label = "Permalink to "使用反汇编"" > < / a > < / h4 > < p > 在某些情况下,你可能需要查看反汇编代码来更深入地理解变量的存储方式。使用以下命令可以查看当前函数的反汇编代码:< / p > < ul > < li > < code > disassemble< / code > :反汇编当前函数的代码。< / li > < li > < code > x/4wx & a< / code > :查看变量 < code > a< / code > 的内存内容。< / li > < / ul > < h3 id = "总结" tabindex = "-1" > 总结 < a class = "header-anchor" href = "#总结" aria-label = "Permalink to "总结"" > < / a > < / h3 > < p > 在 CLion 中使用 GDB 调试时,通过查看变量值和反汇编代码,可以清楚地区分指针变量和普通变量。指针变量存储地址,而普通变量存储实际的值,通过适当的 GDB 命令可以轻松辨别两者的区别。< / p > < h1 id = "第五章-指针的运算-⭐" tabindex = "-1" > 第五章:指针的运算(⭐) < a class = "header-anchor" href = "#第五章-指针的运算-⭐" aria-label = "Permalink to "第五章:指针的运算(⭐)"" > < / a > < / h1 > < h2 id = "_5-1-概述" tabindex = "-1" > 5.1 概述 < a class = "header-anchor" href = "#_5-1-概述" aria-label = "Permalink to "5.1 概述"" > < / a > < / h2 > < h2 id = "_5-2-总结" tabindex = "-1" > 5.2 总结 < a class = "header-anchor" href = "#_5-2-总结" aria-label = "Permalink to "5.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 > : 通过指针访问它指向的变量<EFBFBD>
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-01 09:09:59 +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-01T07:09:26.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-07 09:20:20 +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 \ " : \ " D b L E 4 H 7 4 \ " , \ " 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 \ " : \ " D _ C G 5 P 4 p \ " , \ " 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 \ " : \ " D 3 O e 8 a O 6 \ " , \ " 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 _ n x Z A Z S \ " , \ " 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 S U j d Z C G \ " , \ " 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 \ " : \ " D 2 p U p L V q \ " , \ " 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 \ " : \ " B B H p t z z _ \ " , \ " n o t e s _ 0 4 _ l i n u x _ 0 1 _ x d x _ i n d e x . m d \ " : \ " B p a 1 U f o _ \ " , \ " n o t e s _ i n d e x . m d \ " : \ " D H Y V 8 j v 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 \ " : \ " 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 \ " : \ " 文 件 操 作 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 1 2 _ x d x / \ " } , { \ " t e x t \ " : \ " 调 试 工 具 和 调 试 技 巧 ( g d b 和 m a k e ) \ " , \ " 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 \ " : \ " 常 用 库 函 数 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 1 4 _ 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 \ " : \ " 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 \ " : \ " 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 \ " : \ " 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 \ " : \ " 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 + + 标 准 库 \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ ] } , { \ " t e x t \ " : \ " Q T 桌 面 开 发 \ " , \ " 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 高 并 发 服 务 器 开 发 \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ ] } ] } , \ " s o c i a l L
2024-07-30 07:13:16 +02:00
< / body >
< / html >