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-01 07:49:31 +02:00
< script type = "module" src = "/c/assets/app.CS3VtTfI.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-01 07:49:31 +02:00
< link rel = "modulepreload" href = "/c/assets/chunks/theme.CNylzcy8.js" >
2024-08-01 04:02:42 +02:00
< link rel = "modulepreload" href = "/c/assets/notes_01_c-basic_06_xdx_index.md.BHgC36RS.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-01 00:43:29 +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-07-30 07:13:16 +02:00
< 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-08-01 00:43:29 +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 > < ul > < li > < / li > < / ul > < h2 id = "_4-2-普通变量和指针变量的区别" tabindex = "-1" > 4.2 普通变量和指针变量的区别 < a class = "header-anchor" href = "#_4-2-普通变量和指针变量的区别" aria-label = "Permalink to "4.2 普通变量和指针变量的区别"" > < / a > < / h2 > < p > 在 CLion 中使用 GDB 调试时,可以通过反编译代码来查看指针变量和普通变量的区别。下面是具体的步骤:< / p > < h3 id = "设置-gdb-调试器" tabindex = "-1" > 设置 GDB 调试器 < a class = "header-anchor" href = "#设置-gdb-调试器" aria-label = "Permalink to "设置 GDB 调试器"" > < / a > < / h3 > < ol > < li > 打开 CLion, 并加载你的项目。< / li > < li > 确保你使用的是带有 GDB 支持的工具链(如 GCC 工具链)。< / li > < li > 在 CLion 的设置中,确保调试器设置为 GDB。< / li > < / ol > < h3
< 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 04:02:42 +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-01T02:00:11.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-01 07:49:31 +02:00
< script > window . _ _VP _HASH _MAP _ _ = JSON . parse ( "{\"index.md\":\"1yCQGbx5\",\"notes_01_c-basic_01_xdx_index.md\":\"C3QTVer_\",\"notes_01_c-basic_02_xdx_index.md\":\"CRi9tFKm\",\"notes_01_c-basic_03_xdx_index.md\":\"9iDoRo0k\",\"notes_01_c-basic_04_xdx_index.md\":\"CXwdkIIc\",\"notes_01_c-basic_05_xdx_index.md\":\"cK6ROpeD\",\"notes_01_c-basic_06_xdx_index.md\":\"BHgC36RS\",\"notes_03_linux_01_xdx_index.md\":\"DposN4Uq\",\"notes_index.md\":\"DHYV8jv1\"}" ) ; 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\":\"Linux\",\"collapsed\":true,\"items\":[{\"text\":\"Linux 初识和安装\",\"link\":\"/notes/03_linux/01_xdx/\"}]},{\"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 >