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-07-31 03:20:24 +02:00
< script type = "module" src = "/c/assets/app.ts20Kuhf.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-07-31 03:20:24 +02:00
< link rel = "modulepreload" href = "/c/assets/chunks/theme.DnXN9LWS.js" >
< link rel = "modulepreload" href = "/c/assets/notes_01_c-basic_06_xdx_index.md.DMMIRQE2.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 >
< 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<!-- ] - -
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 项目名称和版本号< / span > < / span >
< span class = "line" > < span > project(c-study VERSION 1.0 LANGUAGES C)< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 设置 C 标准< / span > < / span >
< span class = "line" > < span > set(CMAKE_C_STANDARD 23)< / span > < / span >
< span class = "line" > < span > set(CMAKE_C_STANDARD_REQUIRED True)< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 辅助函数,用于递归查找所有源文件< / span > < / span >
< span class = "line" > < span > function(collect_sources result dir)< / span > < / span >
< span class = "line" > < span > file(GLOB_RECURSE new_sources " ${dir}/*.c" )< / span > < / span >
< span class = "line" > < span > set(${result} ${${result}} ${new_sources} PARENT_SCOPE)< / span > < / span >
< span class = "line" > < span > endfunction()< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 查找顶层 include 目录(如果存在)< / span > < / span >
< span class = "line" > < span > if (EXISTS " ${CMAKE_SOURCE_DIR}/include" )< / span > < / span >
< span class = "line" > < span > include_directories(${CMAKE_SOURCE_DIR}/include)< / span > < / span >
< span class = "line" > < span > endif ()< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 查找所有源文件< / span > < / span >
< span class = "line" > < span > set(SOURCES)< / span > < / span >
< span class = "line" > < span > collect_sources(SOURCES ${CMAKE_SOURCE_DIR})< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 用于存储已经处理过的可执行文件名,防止重复< / span > < / span >
< span class = "line" > < span > set(EXECUTABLE_NAMES)< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 创建可执行文件< / span > < / span >
< span class = "line" > < span > foreach (SOURCE ${SOURCES})< / span > < / span >
< span class = "line" > < span > # 获取文件的相对路径< / span > < / span >
< span class = "line" > < span > file(RELATIVE_PATH REL_PATH ${CMAKE_SOURCE_DIR} ${SOURCE})< / span > < / span >
< span class = "line" > < span > # 将路径中的斜杠替换为下划线,生成唯一的可执行文件名< / span > < / span >
< span class = "line" > < span > string(REPLACE " /" " _" EXECUTABLE_NAME ${REL_PATH})< / span > < / span >
< span class = "line" > < span > string(REPLACE " \\" " _" EXECUTABLE_NAME ${EXECUTABLE_NAME})< / span > < / span >
< span class = "line" > < span > string(REPLACE " ." " _" EXECUTABLE_NAME ${EXECUTABLE_NAME})< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 处理与 CMakeLists.txt 文件同名的问题< / span > < / span >
< span class = "line" > < span > if (${EXECUTABLE_NAME} STREQUAL " CMakeLists_txt" )< / span > < / span >
< span class = "line" > < span > set(EXECUTABLE_NAME " ${EXECUTABLE_NAME}_exec" )< / span > < / span >
< span class = "line" > < span > endif ()< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 检查是否已经创建过同名的可执行文件< / span > < / span >
< span class = "line" > < span > if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)< / span > < / span >
< span class = "line" > < span > list(APPEND EXECUTABLE_NAMES ${EXECUTABLE_NAME})< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 创建可执行文件< / span > < / span >
< span class = "line" > < span > add_executable(${EXECUTABLE_NAME} ${SOURCE})< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 查找源文件所在的目录,并添加为包含目录(头文件可能在同一目录下)< / span > < / span >
< span class = "line" > < span > get_filename_component(DIR ${SOURCE} DIRECTORY)< / span > < / span >
< span class = "line" > < span > target_include_directories(${EXECUTABLE_NAME} PRIVATE ${DIR})< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 检查并添加子目录中的 include 目录(如果存在)< / span > < / span >
< span class = "line" > < span > if (EXISTS " ${DIR}/include" )< / span > < / span >
< span class = "line" > < span > target_include_directories(${EXECUTABLE_NAME} PRIVATE ${DIR}/include)< / span > < / span >
< span class = "line" > < span > endif ()< / span > < / span >
< span class = "line" > < span > < / span > < / span >
< span class = "line" > < span > # 检查并添加 module 目录中的所有 C 文件(如果存在)< / span > < / span >
< span class = "line" > < span > if (EXISTS " ${DIR}/module" )< / span > < / span >
< span class = "line" > < span > file(GLOB_RECURSE MODULE_SOURCES " ${DIR}/module/*.c" )< / span > < / span >
< span class = "line" > < span > target_sources(${EXECUTABLE_NAME} PRIVATE ${MODULE_SOURCES})< / span > < / span >
< span class = "line" > < span > endif ()< / span > < / span >
< span class = "line" > < span > endif ()< / span > < / span >
2024-07-31 03:20:24 +02:00
< span class = "line" > < span > endforeach ()< / 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 > < span class = "line-number" > 20< / span > < br > < span class = "line-number" > 21< / span > < br > < span class = "line-number" > 22< / span > < br > < span class = "line-number" > 23< / span > < br > < span class = "line-number" > 24< / span > < br > < span class = "line-number" > 25< / span > < br > < span class = "line-number" > 26< / span > < br > < span class = "line-number" > 27< / span > < br > < span class = "line-number" > 28< / span > < br > < span class = "line-number" > 29< / span > < br > < span class = "line-number" > 30< / span > < br > < span class = "line-number" > 31< / span > < br > < span class = "line-number" > 32< / span > < br > < span class = "line-number" > 33< / span > < br > < span class = "line-number" > 34< / span > < br > < span class = "line-number" > 35< / span > < br > < span class = "line-number" > 36< / span > < br > < span class = "line-number" > 37< / span > < br > < span class = "line-number" > 38< / span > < br > < span class = "line-number" > 39< / span > < br > < span class = "line-number" > 40< / span > < br > < span class = "line-number" > 41< / span > < br > < span class = "line-number" > 42< / span > < br > < span class = "line-number" > 43< / span > < br > < span class = "line-number" > 44< / span > < br > < span class = "line-number" > 45< / span > < br > < span class = "line-number" > 46< / span > < br > < span class = "line-number" > 47< / span > < br > < span class = "line-number" > 48< / span > < br > < span class = "line-number" > 49< / span > < br > < span class = "line-number" > 50< / span > < br > < span class = "line-number" > 51< / span > < br > < span class = "line-number" > 52< / span > < br > < span class = "line-number" > 53< / span > < br > < span class = "line-number" > 54< / span > < br > < span class = "line-number" > 55< / span > < br > < span class = "line-number" > 56< / span > < br > < span class = "line-number" > 57< / span > < br > < span class = "line-number" > 58< / span > < br > < span class = "line-number" > 59< / span > < br > < span class = "line-number" > 60< / span > < br > < span class = "line-number" > 61< / span > < br > < span class = "line-number" > 62< / span > < br > < span class = "line-number" > 63< / span > < br > < span class = "line-number" > 64< / span > < br > < / div > < / div > < ul > < li > 在 CLion 中正常运行代码:< / li > < / ul > < p > < img src = "/c/assets/9.DdSfz-Pf.gif" alt = "" > < / p > < ul > < li > 在 CLion 中通过 valgrind 运行代码:< / li > < / ul > < p > < img src = "/c/assets/10.BMocjVt7.gif" alt = "" > < / p > < 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 > < 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 > 算术运算< /s
< 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 >
< 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-07-31T01:19:43.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 >
< script > window . _ _VP _HASH _MAP _ _ = JSON . parse ( "{\"index.md\":\"BNNAnhfk\",\"notes_01_c-basic_01_xdx_index.md\":\"DQ3v2Fil\",\"notes_01_c-basic_02_xdx_index.md\":\"BJ3pJ0Cz\",\"notes_01_c-basic_03_xdx_index.md\":\"DIl_eIAg\",\"notes_01_c-basic_04_xdx_index.md\":\"CwyFE3Gl\",\"notes_01_c-basic_05_xdx_index.md\":\"BHSBkPcK\",\"notes_01_c-basic_06_xdx_index.md\":\"DMMIRQE2\",\"notes_index.md\":\"qg0s9W8s\"}" ) ; window . _ _VP _SITE _DATA _ _ = JSON . parse ( "{\"lang\":\"zh-CN\",\"dir\":\"ltr\",\"title\":\"许大仙\",\"titleTemplate\":\"Hi, 终于等到你\",\"description\":\"许大仙前端、Java、大数据、云原生\",\"base\":\"/c/\",\"head\":[],\"router\":{\"prefetchLinks\":true},\"appearance\":true,\"themeConfig\":{\"lastUpdatedText\":\"上次更新\",\"returnToTopLabel\":\"返回顶部\",\"search\":{\"provider\":\"local\"},\"logo\":\"/logo.svg\",\"nav\":[{\"text\":\"首页\",\"link\":\"https://aexiar.github.io/\"},{\"text\":\"计组6件套\",\"link\":\"https://aexiar.github.io/coa6/notes/\"},{\"text\":\"编程语言\",\"items\":[{\"text\":\"c/c++\",\"link\":\"/notes/\"},{\"text\":\"前端\",\"link\":\"https://aexiar.github.io/web-design/notes/\"},{\"text\":\"Java\",\"link\":\"https://aexiar.github.io/java/notes/\"},{\"text\":\"大数据\",\"link\":\"https://aexiar.github.io/big-data/notes/\"},{\"text\":\"云原生\",\"link\":\"https://aexiar.github.io/linux/notes/\"},{\"text\":\"Go\",\"link\":\"https://aexiar.github.io/go/notes/\"},{\"text\":\"Python\",\"link\":\"https://aexiar.github.io/python/notes/\"}]},{\"text\":\"开源软件\",\"link\":\"https://aexiar.github.io/open-software/notes/\"}],\"sidebar\":{\"/notes/\":[{\"text\":\"C 语言基础\",\"collapsed\":true,\"items\":[{\"text\":\"C 语言入门\",\"link\":\"/notes/01_c-basic/01_xdx/\"},{\"text\":\"变量和进制\",\"link\":\"/notes/01_c-basic/02_xdx/\"},{\"text\":\"数据类型和运算符\",\"link\":\"/notes/01_c-basic/03_xdx/\"},{\"text\":\"流程控制\",\"link\":\"/notes/01_c-basic/04_xdx/\"},{\"text\":\"数组\",\"link\":\"/notes/01_c-basic/05_xdx/\"},{\"text\":\"指针\",\"link\":\"/notes/01_c-basic/06_xdx/\"},{\"text\":\"函数\",\"link\":\"/notes/01_c-basic/07_xdx/\"},{\"text\":\"预处理器\",\"link\":\"/notes/01_c-basic/08_xdx/\"},{\"text\":\"自定义数据类型\",\"link\":\"/notes/01_c-basic/09_xdx/\"},{\"text\":\"字符串和时间\",\"link\":\"/notes/01_c-basic/10_xdx/\"},{\"text\":\"内存管理\",\"link\":\"/notes/01_c-basic/11_xdx/\"},{\"text\":\"文件操作\",\"link\":\"/notes/01_c-basic/12_xdx/\"},{\"text\":\"调试工具和调试技巧( gdb和make) \",\"link\":\"/notes/01_c-basic/13_xdx/\"},{\"text\":\"常用库函数\",\"link\":\"/notes/01_c-basic/14_xdx/\"}]},{\"text\":\"C 语言高级\",\"collapsed\":true,\"items\":[]},{\"text\":\"C++ 核心编程\",\"collapsed\":true,\"items\":[]},{\"text\":\"C++ 标准库\",\"collapsed\":true,\"items\":[]},{\"text\":\"QT 桌面开发\",\"collapsed\":true,\"items\":[]},{\"text\":\"Linux 高并发服务器开发\",\"collapsed\":true,\"items\":[]}]},\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/Aurorxa\"}],\"docFooter\":{\"prev\":\"上一篇\",\"next\":\"下一篇\"},\"footer\":{\"message\":\"Released under the MIT License.\",\"copyright\":\"Copyright © 2024 许大仙\"},\"outline\":{\"level\":[1,6],\"label\":\"目录\"},\"outlineTitle\":\"当前页大纲\"},\"locales\":{},\"scrollOffset\":134,\"cleanUrls\":false}" ) ; < / script >
2024-07-30 07:13:16 +02:00
< / body >
< / html >