2024-07-24 09:39:31 +02:00
<!DOCTYPE html>
< html lang = "zh-CN" dir = "ltr" >
< head >
< meta charset = "utf-8" >
< title > 第一章:数组的概念 | Hi, 终于等到你< / title >
< meta name = "description" content = "许大仙前端、Java、大数据、云原生" >
2024-08-09 04:52:37 +02:00
< meta name = "generator" content = "VitePress v1.3.2" >
< link rel = "preload stylesheet" href = "/c/assets/style.8enJ9bsK.css" as = "style" >
2024-07-24 09:39:31 +02:00
2024-08-12 02:36:26 +02:00
< script type = "module" src = "/c/assets/app.h_VhotEN.js" > < / script >
2024-07-24 09:39:31 +02:00
< link rel = "preload" href = "/c/assets/inter-roman-latin.Di8DUHzh.woff2" as = "font" type = "font/woff2" crossorigin = "" >
2024-08-12 02:36:26 +02:00
< link rel = "modulepreload" href = "/c/assets/chunks/theme.ozwS6Ghe.js" >
2024-08-09 04:52:37 +02:00
< link rel = "modulepreload" href = "/c/assets/chunks/framework.CZRoMP2i.js" >
2024-08-11 09:02:51 +02:00
< link rel = "modulepreload" href = "/c/assets/notes_01_c-basic_05_xdx_index.md.CBiiY4-B.lean.js" >
2024-07-24 09:39:31 +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-09 08:48:23 +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-24 09:39:31 +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:#24292E;--shiki-dark:#E1E4E8;" > < / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num1 < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num2 < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num3 < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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 >
< 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;" > " 请输入第 1 个员工的工资:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > scanf< / 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;" > %lf< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num1);< / 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;" > " 请输入第 2 个员工的工资:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > scanf< / 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;" > %lf< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num2);< / 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;" > " 请输入第 3 个员工的工资:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > scanf< / 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;" > %lf< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num3);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ... < / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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 > < 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 > < / div > < / div > < ul > < li > 这样会感觉特别机械和麻烦( 全是复制( Ctrl + c) 和粘贴( Ctrl + v) , CV 大法);此时,我们就可以将所有的< code > 数据< / code > 全部存储到一个< code > 容器(数组)< / code > 中进行统一管理,并进行其它的操作,如:求最值、求平均值等,如下所示:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< 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:#6A737D;--shiki-dark:#6A737D;" > // 声明数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > nums< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 50< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (nums) < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 使用 for 循环向数组中添加值< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length;i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ){< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 请输入第 & d 个员工的工资:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,i);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > scanf< / 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;" > %lf< / 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;" > num< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 其它操作,如:求最值,求平均值等< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ... < / 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 >
2024-07-24 09:45:01 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < / div > < / div > < h3 id = "_1-1-2-需求分析-2" tabindex = "-1" > 1.1.2 需求分析 2 < a class = "header-anchor" href = "#_1-1-2-需求分析-2" aria-label = "Permalink to "1.1.2 需求分析 2"" > < / a > < / h3 > < ul > < li > 在现实生活中,我们会使用很多 APP 或微信小程序等,即:< / li > < / ul > < p > < img src = "/c/assets/1.CvTjP5jN.png" alt = "" > < / p > < ul > < li > 同样的道理,如果我们使用变量来存储每个商品信息,那么就需要非常多的变量;但是,如果我们将这些< code > 商品信息< / code > 都存储到一个< code > 容器(数组)< / code > 中,进行统一管理;那么,之后的数据处理将会非常方便。< / li > < / ul > < h3 id = "_1-1-3-容器的概念" tabindex = "-1" > 1.1.3 容器的概念 < a class = "header-anchor" href = "#_1-1-3-容器的概念" aria-label = "Permalink to "1.1.3 容器的概念"" > < / a > < / h3 > < ul > < li > < code > 生活中的容器< / code > :水杯(装水、饮料的容器)、衣柜(装衣服等物品的容器)、集装箱(装货物等物品的容器)。< / li > < li > < code > 程序中的容器< / code > :将多个数据存储到一起,并且每个数据称为该容器中的元素。< / li > < / ul > < h2 id = "_1-2-什么是数组" tabindex = "-1" > 1.2 什么是数组? < a class = "header-anchor" href = "#_1-2-什么是数组" aria-label = "Permalink to "1.2 什么是数组?"" > < / a > < / h2 > < ul > < li > 数组( Array) 是将多个< code > 相同数据类型< / code > 的< code > 数据< / code > 按照一定的顺序排序的< code > 集合< / code > ,并使用一个< code > 标识符< / code > 命名,以及通过< code > 编号(索引,亦称为下标)< / code > 的方式对这些数据进行统一管理。< / li > < / ul > < p > < img src = "/c/assets/2.BR4TvWzU.png" alt = "" > < / p > < h2 id = "_1-3-数组的相关概念" tabindex = "-1" > 1.3 数组的相关概念 < a class = "header-anchor" href = "#_1-3-数组的相关概念" aria-label = "Permalink to "1.3 数组的相关概念"" > < / a > < / h2 > < ul > < li > < code > 数组名< / code > :本质上是一个标识符常量,命名需要符合标识符规则和规范。< / li > < li > < code > 元素< / code > :同一个数组中的元素必须是相同的数据类型。< / li > < li > < code > 索引(下标)< / code > :从 0 开始的连续数字。< / li > < li > < code > 数组的长度< / code > :就是元素的个数。< / li > < / ul > < h2 id = "_1-4-数组的特点" tabindex = "-1" > 1.4 数组的特点 < a class = "header-anchor" href = "#_1-4-数组的特点" aria-label = "Permalink to "1.4 数组的特点"" > < / a > < / h2 > < ul > < li > ① 创建数组的时候,会在内存中开辟一整块< code > 连续的空间< / code > ,占据空间的大小,取决于数组的长度和数组中元素的类型。< / li > < li > ② 数组中的元素在内存中是依次紧密排列且有序的。< / li > < li > ③ 数组一旦初始化完成,且长度就确定的,并且< code > 数组的长度一旦确定,就不能更改< / code > 。< / li > < li > ④ 我们可以直接通过索引(下标)来获取指定位置的元素,速度很快。< / li > < li > ⑤ 数组名中引用的是这块连续空间的首地址。< / li > < / ul > < h1 id = "第二章-数组的操作-⭐" tabindex = "-1" > 第二章:数组的操作(⭐) < a class = "header-anchor" href = "#第二章-数组的操作-⭐" aria-label = "Permalink to "第二章:数组的操作(⭐)"" > < / a > < / h1 > < h2 id = "_2-1-数组的定义" tabindex = "-1" > 2.1 数组<E695B0> <E7BB84>
2024-07-24 09:39:31 +02:00
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 先指定元素的个数和类型,再进行初始化< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 给数组元素赋值< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#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:#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:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 20< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 30< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < / div > < / div > < h3 id = "_2-1-2-静态初始化-1" tabindex = "-1" > 2.1.2 静态初始化 1 < a class = "header-anchor" href = "#_2-1-2-静态初始化-1" aria-label = "Permalink to "2.1.2 静态初始化 1"" > < / a > < / h3 > < ul > < li > 语法:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > 数据类型 数组名[元素个数< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > |< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > 长度] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {元素1,元素2,...}< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < / div > < / div > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① 静态部分初始化:如果数组初始化的元素个数< code > 小于< / code > 数组声明的长度,那么就会从数组开始位置依次赋值,不够的就补 0 。< / li > < li > ② 静态全部初始化:数组初始化的元素个数< code > 等于< / code > 数组的长度。< / li > < / ul > < / div > < ul > < li > < p > 技巧:< / p > < ul > < li > 在 CLion 中可以开启< code > 聚合初始化< / code > 功能,即:< / li > < / ul > < p > < img src = "/c/assets/3.hr3pCY5b.png" alt = "" > < / p > < ul > < li > 这样,在 CLion 中,将会显示数组初始化中的元素索引,即:< / li > < / ul > < p > < img src = "/c/assets/4.CgvWfxq4.png" alt = "" > < / p > < / li > < li > < p > 示例:静态部分初识化< / p > < / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组和部分初始化:< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 会将给定的值从数组的开始位置一个个的赋值,没有赋值的地方,用 0 填充< / span > < / span >
< 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 style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < / div > < / div > < ul > < li > 示例:静态全部初始化< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组和全部初始化:数组初始化的元素个数等于数组的长度。< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#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 style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 >
< 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 > < span class = "line-number" > 9< / span > < br > < / div > < / div > < h3 id = "_2-1-3-静态初始化-2" tabindex = "-1" > 2.1.3 静态初始化 2 < a class = "header-anchor" href = "#_2-1-3-静态初始化-2" aria-label = "Permalink to "2.1.3 静态初始化 2"" > < / a > < / h3 > < ul > < li > 语法:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > 数据类型 数组名< / 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;" > {元素1,元素2,...}< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < / div > < / div > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 没有给出数组中元素的个数,将由系统根据初始化的元素,自动推断出数组中元素的个数。< / p > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 指定元素的类型,不指定元素个数,同时进行初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 >
< 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 > < span class = "line-number" > 9< / span > < br > < / div > < / div > < h2 id = "_2-2-访问数组元素" tabindex = "-1" > 2.2 访问数组元素 < a class = "header-anchor" href = "#_2-2-访问数组元素" aria-label = "Permalink to "2.2 访问数组元素"" > < / a > < / h2 > < ul > < li > 语法:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > 数组名[索引< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > |< / span > < 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 > < / div > < / div > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 假设数组 < code > arr< / code > 有 n 个元素,如果使用的数组的下标 < code > < 0< / code > 或 < code > > n-1< / code > ,那么将会产生数组越界访问,即超出了数组合法空间的访问;那么,数组的索引范围是 < code > [0,arr.length - 1]< / code > 。< / p > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 先指定元素的个数和类型,再进行初始化< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 给数组元素赋值< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#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:#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:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 20< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 30< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 访问数组元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " arr[0] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#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;" > // arr[0] = 10< / 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;" > " arr[1] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[1] = 20< / 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;" > " arr[2] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[2] = 30< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < span class = "line-number" > 20< / span > < br > < span class = "line-number" > 21< / span > < br > < / div > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组和部分初始化:< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 会将给定的值从数组的开始位置一个个的赋值,没有赋值的地方,用 0 填充< / span > < / span >
< 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 style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 访问数组元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " arr[0] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#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;" > // arr[0] = 1< / 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;" > " arr[1] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[1] = 2< / 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;" > " arr[2] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[2] = 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;" > " arr[3] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[3] = 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;" > " arr[4] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[4] = 0< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < / div > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 指定元素的类型,不指定元素个数,同时进行初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 访问数组元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " arr[0] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#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;" > // arr[0] = 1< / 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;" > " arr[1] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[1] = 2< / 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;" > " arr[2] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[2] = 3< / 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;" > " arr[3] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[3] = 4< / 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;" > " arr[4] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[4] = 5< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < / div > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组和全部初始化:数组初始化的元素个数等于数组的长度。< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#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 style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 访问数组元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " arr[0] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#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;" > // arr[0] = 1< / 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;" > " arr[1] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[1] = 2< / 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;" > " arr[2] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[2] = 3< / 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;" > " arr[3] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[3] = 4< / 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;" > " arr[4] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[4] = 5< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < / div > < / div > < h2 id = "_2-3-数组越界" tabindex = "-1" > 2.3 数组越界 < a class = "header-anchor" href = "#_2-3-数组越界" aria-label = "Permalink to "2.3 数组越界"" > < / a > < / h2 > < ul > < li > 数组下标必须在指定范围内使用,超出范围视为越界。< / li > < / ul > < p > < img src = "/c/assets/5.DSekQATc.png" alt = "" > < / p > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① C 语言是不会做数组下标越界的检查,并且编译器也不会报错;但是,编译器不报错,并不意味着程序就是正确!< / li > < li > ② 在其它高级编程语言, 如: Java、JavaScript、Rust 等中,如果数组越界访问,编译器是会直接报错的!!!< / li > < / ul > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组和全部初始化:数组初始化的元素个数等于数组的长度。< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 访问数组元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " arr[0] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#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;" > // arr[0] = 1< / 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;" > " arr[1] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[1] = 2< / 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;" > " arr[2] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[2] = 3< / 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;" > " arr[3] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[3] = 4< / 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;" > " arr[4] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[4] = 5< / 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;" > " arr[-1] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / 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;" > " arr[5] = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#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 style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 得到的是不确定的结果< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < / div > < / div > < h2 id = "_2-4-计算数组的长度" tabindex = "-1" > 2.4 计算数组的长度 < a class = "header-anchor" href = "#_2-4-计算数组的长度" aria-label = "Permalink to "2.4 计算数组的长度"" > < / a > < / h2 > < ul > < li > 数组长度(元素个数)是在数组定义的时候明确指定且固定的,我们不能在运行的时候直接获取数组长度;但是,我们可以通过 sizeof 运算符间接计算出数组的长度。< / li > < li > 计算步骤,如下所示: < ul > < li > ① 使用 sizeof 运算符计算出整个数组的字节长度。< / li > < li > ② 由于数组成员是同一数据类型;那么,每个元素的字节长度一定相等,那么< code > 数组的长度 = 整个数组的字节长度 ÷ 单个元素的字节长度 < / code > 。< / li > < / ul > < / li > < / ul > < p > < img src = "/c/assets/6.DjtEKfCa.png" alt = "" > < / p > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① 在很多编程语言中, 都内置了获取数组的长度的属性或方法, 如: Java 中的 arr.length 或 Rust 的 arr.len()。< / li > < li > ② 但是, C 语言没有内置的获取数组长度的属性或方法,只能通过 sizeof 运算符间接来计算得到。< / li > < li > ③ 数组一旦< code > 声明< / code > 或< code > 定义< / code > ,其< code > 长度< / code > 就< code > 固定< / code > 了,< code > 不能动态变化< / code > 。< / li > < / ul > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组和全部初始化:数组初始化的元素个数等于数组的长度。< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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 >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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 > < 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 > < / div > < / div > < h2 id = "_2-5-遍历数组" tabindex = "-1" > 2.5 遍历数组 < a class = "header-anchor" href = "#_2-5-遍历数组" aria-label = "Permalink to "2.5 遍历数组"" > < / a > < / h2 > < ul > < li > < p > 遍历数组是指按顺序访问数组中的每个元素,以便读取或修改它们,编程中一般使用循环结构对数组进行遍历。< / p > < / li > < li > < p > 示例:声明一个存储有 12、2、31、24、15、36、67、108、29、51 的数组,并遍历数组所有元素< / p > < / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 15< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 108< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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 > < 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 > < / div > < / div > < ul > < li > 示例:声明长度为 10 的 int 类型数组,给数组元素依次赋值为 0 ~ 9 ,并遍历数组所有元素< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 给数组的每个元素赋值< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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 > < 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 > < / div > < / div > < h2 id = "_2-6-一维数组的内存分析" tabindex = "-1" > 2.6 一维数组的内存分析 < a class = "header-anchor" href = "#_2-6-一维数组的内存分析" aria-label = "Permalink to "2.6 一维数组的内存分析"" > < / a > < / h2 > < h3 id = "_2-6-1-数组内存图" tabindex = "-1" > 2.6.1 数组内存图 < a class = "header-anchor" href = "#_2-6-1-数组内存图" aria-label = "Permalink to "2.6.1 数组内存图"" > < / a > < / h3 > < ul > < li > 假设数组是如下的定义:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < / div > < / div > < ul > < li > 那么,对应的内存结构,如下所示:< / li > < / ul > < p > < img src = "/c/assets/7.BTWPdsCm.png" alt = "" > < / p > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① 数组名 < code > arr< / code > 就是记录该数组的首地址,即 < code > arr[0]< / code > 的地址。< / li > < li > ② 数组中的各个元素是连续分布的,假设 < code > arr[0]< / code > 的地址是 < code > 0xdea7bff880< / code > ,则 < code > arr[1] 的地址 = arr[0] 的地址 + int 字节数( 4) = 0xdea7bff880 + 4 = 0xdea7bff884< / code > ,依次类推...< / li > < / ul > < / div > < ul > < li > 在 C 语言中,我们可以通过 < code > & arr< / code > 或 < code > & arr[0]< / code > 等形式获取数组或数组元素的地址,即:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 给数组的每个元素赋值< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#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 >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 数组元素 < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / 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:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i], < / 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;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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 > < 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 > < / div > < / div > < h3 id = "_2-6-2-数组的注意事项" tabindex = "-1" > 2.6.2 数组的注意事项 < a class = "header-anchor" href = "#_2-6-2-数组的注意事项" aria-label = "Permalink to "2.6.2 数组的注意事项"" > < / a > < / h3 > < ul > < li > < code > C 语言规定,数组一旦声明,数组名指向的地址将不可更改< / code > 。因为在声明数组的时候,编译器会自动会数组分配内存地址,这个地址和数组名是绑定的,不可更改。< / li > < / ul > < div class = "warning custom-block github-alert" > < p class = "custom-block-title" > WARNING< / p > < p > < / p > < p > 如果之后试图更改数组名对应的地址,编译器就会报错。< / p > < / div > < ul > < li > 示例:错误演示< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > num< / 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 style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 声明数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 使用大括号重新赋值是不允许的,必须在数组声明的时候赋值,否则编译将会报错< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 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 > < / div > < / div > < ul > < li > 示例:错误演示< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > []< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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:#6A737D;--shiki-dark:#6A737D;" > // 使用大括号重新赋值是不允许的,必须在数组声明的时候赋值,否则编译将会报错< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 6< / 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 > < / div > < / div > < ul > < li > 示例:错误演示< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > num< / 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 >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > NULL< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 报错,需要和 Java 区别一下,在 C 中不可以< / 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 > < / div > < / div > < ul > < li > 示例:错误演示< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > b< / 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 style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > a ;< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 报错,需要和 Java 区别一下,在 C 中不可以< / 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 > < / div > < / div > < h2 id = "_2-7-数组应用案例" tabindex = "-1" > 2.7 数组应用案例 < a class = "header-anchor" href = "#_2-7-数组应用案例" aria-label = "Permalink to "2.7 数组应用案例"" > < / a > < / h2 > < h3 id = "_2-7-1-应用示例" tabindex = "-1" > 2.7.1 应用示例 < a class = "header-anchor" href = "#_2-7-1-应用示例" aria-label = "Permalink to "2.7.1 应用示例"" > < / a > < / h3 > < ul > < li > < p > 需求:计算数组中所有元素的和以及平均数。< / p > < / li > < li > < p > 示例:< / p > < / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 15< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 108< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 变量保存总和< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > sum < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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 >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > sum < / 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;" > [i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > avg < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > )sum < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 数组的和为:< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , sum);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 数组的和为: 375< / 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;" > %.2lf\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , avg);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > //数组的平均值为: 37.50< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < 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 > < / div > < / div > < h3 id = "_2-7-2-应用示例" tabindex = "-1" > 2.7.2 应用示例 < a class = "header-anchor" href = "#_2-7-2-应用示例" aria-label = "Permalink to "2.7.2 应用示例"" > < / a > < / h3 > < ul > < li > 需求:计算数组的最值(最大值和最小值)。< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 思路:< / p > < ul > < li > ① 假设数组中的第一个元素是最大值或最小值,并使用变量 max 或 min 保存。< / li > < li > ② 遍历数组中的每个元素: < ul > < li > 如果有元素比最大值还要大,就让变量 max 保存最大值。< / li > < li > 如果有元素比最小值还要小,就让变量 min 保存最小值。< / li > < / ul > < / li > < / ul > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 15< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 108< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义最大值< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > max < / 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 >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义最小值< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > min < / 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 >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > if< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > max) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > max < / 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;" > [i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > if< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > min) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > min < / 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;" > [i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 数组的最大值为:< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , max);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 数组的最大值为: 108< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 数组的最小值为:< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , min);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 数组的最小值为:-36< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < 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 > < / div > < / div > < h3 id = "_2-7-3-应用示例" tabindex = "-1" > 2.7.3 应用示例 < a class = "header-anchor" href = "#_2-7-3-应用示例" aria-label = "Permalink to "2.7.3 应用示例"" > < / a > < / h3 > < ul > < li > < p > 需求:统计数组中某个元素出现的次数,要求:使用无限循环,如果输入的数字是 0 ,就退出。< / p > < / li > < li > < p > 示例:< / p > < / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 108< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 当前数组中的元素是:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#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;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 无限循环< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > while< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > true< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 统计的数字< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 统计数字出现的次数< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > count < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#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:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > scanf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 0 作为结束条件< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > if< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (num < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ==< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > break< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组,并计数< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > if< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ==< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > num) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > count< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 您输入的数字 < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > 在数组中出现了 < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > 次< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , num, count);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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 > < 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 > < / div > < / div > < h3 id = "_2-7-4-应用示例" tabindex = "-1" > 2.7.4 应用示例 < a class = "header-anchor" href = "#_2-7-4-应用示例" aria-label = "Permalink to "2.7.4 应用示例"" > < / a > < / h3 > < ul > < li > < p > 需求:将数组 a 中的全部元素复制到数组 b 中。< / p > < / li > < li > < p > 示例:< / p > < / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #define< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > SIZE< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义数组并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > a< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 15< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 108< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / 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:#E36209;--shiki-dark:#FFAB70;" > b< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [SIZE];< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 复制数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > b< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > a< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印数组 b 中的全部元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > b< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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 > < 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 > < / div > < / div > < h3 id = "_2-7-5-应用示例" tabindex = "-1" > 2.7.5 应用示例 < a class = "header-anchor" href = "#_2-7-5-应用示例" aria-label = "Permalink to "2.7.5 应用示例"" > < / a > < / h3 > < ul > < li > 需求:数组对称位置的元素互换。< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 思路:假设数组一共有 10 个元素,那么:< / p > < ul > < li > a[0] 和 a[9] 互换。< / li > < li > a[1] 和 a[8] 互换。< / li > < li > ...< / li > < / ul > < p > 规律就是 < code > a[i] < --互换--> arr[arr.length -1 -i]< / code > < / p > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 原始数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 15< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 108< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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 >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > < / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印原始数组中的全部元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 原始数组:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 交换数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / 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;" > temp < / 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;" > [i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / 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;" > [SIZE < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [SIZE < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > temp;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#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:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < 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 > < / div > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 原始数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 15< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 108< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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 >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印原始数组中的全部元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 原始数组:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 交换数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , j< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > --< / 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;" > temp < / 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;" > [i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / 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;" > [j];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [j] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > temp;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#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:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > SIZE; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < 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 > < / div > < / div > < h3 id = "_2-7-6-应用示例" tabindex = "-1" > 2.7.6 应用示例 < a class = "header-anchor" href = "#_2-7-6-应用示例" aria-label = "Permalink to "2.7.6 应用示例"" > < / a > < / h3 > < ul > < li > 需求:将数组中的最大值移动到数组的最末尾。< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 思路:从数组的下标 < code > 0< / code > 开始依次遍历到 < code > length - 1< / code > ,如果 < code > i< / code > 下标当前的值比 < code > i+1< / code > 下标的值大,则交换;否则,就不交换。< / p > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 原始数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 15< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 891< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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 >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印原始数组中的全部元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 原始数组:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 移动最大值到数组的最后一个位置< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > if< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / 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;" > [i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / 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;" > temp < / 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;" > [i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / 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;" > [i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > temp;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#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:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < 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 > < / div > < / div > < h3 id = "_2-7-7-应用示例" tabindex = "-1" > 2.7.7 应用示例 < a class = "header-anchor" href = "#_2-7-7-应用示例" aria-label = "Permalink to "2.7.7 应用示例"" > < / a > < / h3 > < ul > < li > 需求:实现冒泡排序,即将数组的元素从小到大排列。< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 思路:一层循环,能实现最大值移动到数组的最后;那么,二层循环(控制内部循环数组的长度)就能实现将数组的元素从小到大排序。< / p > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 原始数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 31< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 24< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 15< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 36< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 67< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 891< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 29< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 51< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 计算数组的长度< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > size_t< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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 >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印原始数组中的全部元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 原始数组:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; j< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > -< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > j; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > if< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / 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;" > [i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / 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;" > temp < / 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;" > [i];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i] < / 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;" > [i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > temp;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印移动之后的数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 移动之后的数组:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > length; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < 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 > < / div > < / div > < h1 id = "第三章-多维数组-⭐" tabindex = "-1" > 第三章:多维数组(⭐) < a class = "header-anchor" href = "#第三章-多维数组-⭐" aria-label = "Permalink to "第三章:多维数组(⭐)"" > < / a > < / h1 > < h2 id = "_3-1-概述" tabindex = "-1" > 3.1 概述 < a class = "header-anchor" href = "#_3-1-概述" aria-label = "Permalink to "3.1 概述"" > < / a > < / h2 > < h3 id = "_3-1-1-引入" tabindex = "-1" > 3.1.1 引入 < a class = "header-anchor" href = "#_3-1-1-引入" aria-label = "Permalink to "3.1.1 引入"" > < / a > < / h3 > < ul > < li > < p > 我们在数学、物理和计算机科学等学科中学习过< code > 一维坐标< / code > 、< code > 二维坐标< / code > 以及< code > 三维坐标< / code > 。< / p > < / li > < li > < p > 其中,< code > 一维坐标< / code > 通常用于描述在线段或直线上的点的位置,主要应用有:< / p > < ul > < li > < strong > 数轴< / strong > :一维坐标可以用来表示数轴上的数值位置,这在基础数学和初等代数中非常常见。< / li > < / ul > < p > < img src = "/c/assets/8.EoNXwZXz.png" alt = "" > < / p > < ul > < li > < strong > 时间轴< / strong > :时间可以看作是一维的,它可以用一维坐标表示,例如:秒、分钟、小时等。< / li > < / ul > < p > < img src = "/c/assets/9.iU-LaFqX.png" alt = "" > < / p > < ul > < li > < strong > 统计数据< / strong > :一维坐标常用于表示单变量的数据集,如:测量身高、体重、温度等。< / li > < / ul > < p > < img src = "/c/assets/10.DYoXU9E6.jpg" alt = "" > < / p > < / li > < li > < p > 其中,< code > 二维坐标< / code > 用于描述平面上的点的位置。主要应用包括:< / p > < ul > < li > < strong > 几何学< / strong > :在几何学中,二维坐标用于表示平面图形的顶点、边和面积等。< / li > < / ul > < p > < img src = "/c/assets/11.DWDH3yqc.png" alt = "" > < / p > < ul > < li > < strong > 地图和导航< / strong > :地理坐标系统(经纬度)使用二维坐标来表示地球表面的任意位置。< / li > < / ul > < p > < img src = "/c/assets/12.BvBjpbbE.png" alt = "image-20240724112326592" > < / p > < ul > < li > < strong > 图形设计和计算机图形学< / strong > :二维坐标在绘制图形、设计图案和用户界面中非常重要。< / li > < / ul > < p > < img src = "/c/assets/13.CmPe90qp.png" alt = "" > < / p > < ul > < li > < strong > 物理学< / strong > :二维运动和场,例如:在描述物体在平面上的运动轨迹时使用二维坐标。< / li > < / ul > < p > < img src = "/c/assets/14.MepLJvOt.jpg" alt = "" > < / p > < / li > < li > < p > 其中,三维坐标用于描述空间中点的位置。主要应用包括:< / p > < ul > < li > < strong > 几何学< / strong > : 三维坐标在空间几何中用于表示立体图形的顶点、边、<EFBFBD> <EFBFBD>
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义二维数组并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {{< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 6< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 7< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 8< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }, {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 9< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 11< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }};< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 输出二维数组中的元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / 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 >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / 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 >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < 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 > < / div > < / div > < h3 id = "_3-2-2-静态初始化-1" tabindex = "-1" > 3.2.2 静态初始化 1 < a class = "header-anchor" href = "#_3-2-2-静态初始化-1" aria-label = "Permalink to "3.2.2 静态初始化 1"" > < / a > < / h3 > < ul > < li > 语法:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > 数据类型 数组名[行数][列数] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {{元素1,元素2,...},{元素3,...},...}< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < / div > < / div > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① 行数 = 几个一维数组元素。< / li > < li > ② 列数 = 每个⼀维数组中有几个具体的数据元素。< / li > < / ul > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< 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 >
2024-07-24 10:04:26 +02:00
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义二维数组并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {{< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 6< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 7< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 8< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }, {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 9< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 11< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }};< / span > < / span >
2024-07-24 09:39:31 +02:00
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 输出二维数组中的元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / 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 >
2024-07-24 10:04:26 +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;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
2024-07-24 09:39:31 +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;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / 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 >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-07-24 10:04:26 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < 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 > < / div > < / div > < h3 id = "_3-2-3-静态初始化-2" tabindex = "-1" > 3.2.3 静态初始化 2 < a class = "header-anchor" href = "#_3-2-3-静态初始化-2" aria-label = "Permalink to "3.2.3 静态初始化 2"" > < / a > < / h3 > < ul > < li > 语法:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > 数据类型 数组名< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > []< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [列数] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {{元素1,元素2,...},{元素3,...},...}< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < / div > < / div > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① 列数 = 每个⼀维数组中有几个具体的数据元素。< / li > < li > ② 可以不指定行数,必须指定列数,编译器会根据元素的个数和列的个数,自动推断出行数!!!< / li > < / ul > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
2024-07-24 09:39:31 +02:00
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义二维数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > []< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {{< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 6< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }, {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 9< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 11< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }};< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 输出二维数组中的元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / 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 >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / 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 >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / 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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < span class = "line-number" > 20< / span > < br > < span class = "line-number" > 21< / span > < br > < / div > < / div > < h2 id = "_3-3-二维数组的理解" tabindex = "-1" > 3.3 二维数组的理解 < a class = "header-anchor" href = "#_3-3-二维数组的理解" aria-label = "Permalink to "3.3 二维数组的理解"" > < / a > < / h2 > < ul > < li > 如果二维数组是这么定义的,即:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > 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;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ][< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < 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 > < / div > < / div > < ul > < li > 那么,这个二维数组 < code > arr< / code > 可以看做是 < code > 3< / code > 个一维数组组成,它们分别是 < code > arr[0]< / code > 、< code > arr[1]< / code > 、< code > arr[2]< / code > 。这 < code > 3< / code > 个一维数组都各有 4 个元素,如:一维数组 < code > arr[0]< / code > 中的元素是 < code > arr[0][0]< / code > 、< code > arr[0][1]< / code > 、< code > arr[0][2]< / code > 、< code > arr[0][3]< / code > ,即:< / li > < / ul > < p > < img src = "/c/assets/20.BvSSXPdI.png" alt = "" > < / p > < h2 id = "_3-4-二维数组的遍历" tabindex = "-1" > 3.4 二维数组的遍历 < a class = "header-anchor" href = "#_3-4-二维数组的遍历" aria-label = "Permalink to "3.4 二维数组的遍历"" > < / a > < / h2 > < ul > < li > 访问二维数组的元素,需要使用两个下标(索引),一个用于访问行(第一维),另一个用于访问列(第二维),我们通常称为行下标(行索引)或列下标(列索引)。< / li > < li > 所以,遍历二维数组,需要使用双层循环结构。< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 如果一个二维数组是这么定义的,即:< code > int arr[3][4]< / code > ,那么:< / p > < ul > < li > < code > 行的长度 = sizeof(arr) / sizeof(arr[0])< / code > ,因为 < code > arr< / code > 是二维数组的< code > 总< / code > 的内存空间;而 < code > arr[0]< / code > 、< code > arr[1]< / code > 、< code > arr[2]< / code > 是二维数组中一维数组的内存空间 。< / li > < li > < code > 列的长度 = sizeof(arr[0]) / sizeof(arr[0][0])< / code > ,因为< code > arr[0]< / code > 、< code > arr[1]< / code > 、< code > arr[2]< / code > 是二维数组中一维数组的内存空间 ,而 < code > arr[0][0]< / code > 、< code > arr[0][1]< / code > 、... 是一维数组中元素的内存空间。< / li > < / ul > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义二维数组< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > []< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ] < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > {{< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 4< / 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 style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 6< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }, {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 9< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 10< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 11< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 12< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }};< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 获取行列数< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > row < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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 >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > col < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 打印二维数组元素< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > row; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > col; j< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > arr< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [i][j]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-07-24 10:58:49 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < span class = "line-number" > 19< / span > < br > < span class = "line-number" > 20< / span > < br > < span class = "line-number" > 21< / span > < br > < / div > < / div > < h2 id = "_3-5-二维数组的内存分析" tabindex = "-1" > 3.5 二维数组的内存分析 < a class = "header-anchor" href = "#_3-5-二维数组的内存分析" aria-label = "Permalink to "3.5 二维数组的内存分析"" > < / a > < / h2 > < ul > < li > < p > 用< code > 矩阵形式< / code > ( 如: 3 行 4 列形式)表示二维数组,是< code > 逻辑< / code > 上的概念,能形象地表示出行列关系。而在< code > 内存< / code > 中,各元素是连续存放的,不是二维的,是< code > 线性< / code > 的。< / p > < / li > < li > < p > C 语言中,二维数组中元素排列的顺序是< code > 按行存放< / code > 的。即:先顺序存放第一行的元素,再存放第二行的元素。例如:数组< code > a[3][4] < / code > 在内存中的存放,如下所示:< / p > < / li > < / ul > < p > < img src = "/c/assets/21.uaya_nVR.png" alt = "" > < / p > < h2 id = "_3-6-二维数组的应用案例" tabindex = "-1" > 3.6 二维数组的应用案例 < a class = "header-anchor" href = "#_3-6-二维数组的应用案例" aria-label = "Permalink to "3.6 二维数组的应用案例"" > < / a > < / h2 > < ul > < li > < p > 需求:现在有三个班,每个班五名同学,用二维数组保存他们的成绩,并求出每个班级平均分、以及所有班级平均分,数据要求从控制台输入。< / p > < / li > < li > < p > 示例:< / p > < / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
2024-07-24 09:39:31 +02:00
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义二维数组,用于保存成绩< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > double< / 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;" > 3< / 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 >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 获取二维数组的行数和列数< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > row < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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 >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > col < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > sizeof< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / 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:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 从控制台输入成绩< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > row; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > col; j< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 请输入第< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > 个班级的第< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > 个学生的成绩:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > scanf< / 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;" > %lf< / 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;" > [i][j]);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 总分< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > totalSum < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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 >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 遍历数组,求总分和各个班级的平均分< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > row; i< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > double< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > sum < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / 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:#D73A49;--shiki-dark:#F97583;" > for< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ; j < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > < < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > col; j< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > ++< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ) {< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > totalSum < / 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;" > [i][j];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > sum < / 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;" > [i][j];< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 第< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > 个班级的总分为:< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %.2lf\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , sum);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > printf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " 第< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > 个班级的平均分为:< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %.2lf\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , i < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > +< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , sum < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > col);< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#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;" > %.2lf\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , totalSum);< / 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;" > %.2lf\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , totalSum < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > /< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (row < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > *< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > col));< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-08-01 11:02:15 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < span class = "line-number" > 16< / span > < br > < span class = "line-number" > 17< / span > < br > < span class = "line-number" > 18< / span > < br > < 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 > < / div > < / div > < 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 > 在实际开发中,我们除了经常处理整数、浮点数、字符等,还经常和字符串打交道,如:< code > " Hello World" < / code > 、< code > " Hi" < / code > 等。< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 像这类< code > " Hello World" < / code > 、< code > " Hi" < / code > 等格式 ,使用双引号引起来的一串字符称为字符串字面值,简称字符串。< / p > < / div > < ul > < li > 对于整数、浮点数和字符, C 语言中都提供了对应的数据类型。但是, 对于字符串, C 语言并没有提供对应的数据类型,而是用< code > 字符数组< / code > 来存储这类文本类型的数据,即字符串:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > char< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > str< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 32< / span > < 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 > < / div > < / div > < ul > < li > 字符串不像整数、浮点数以及字符那样有固定的大小,字符串是不定长的,如:< code > " Hello World" < / code > 、< code > " Hi" < / code > 等的长度就是不一样的。在 C 语言中,规定了字符串的结尾必须是 < code > \0< / code > ,这种字符串也被称为 < code > C 风格的字符串< / code > ,如:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#032F62;
2024-08-01 10:49:10 +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:#6A737D;--shiki-dark:#6A737D;" > // 禁用 stdout 缓冲区< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > setbuf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (stdout, < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > NULL< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 字符数组,不是字符串< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > char< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > c1< / 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;" > {< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' H' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' e' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' l' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' l' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' o' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' ' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' W' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' o' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' r' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' l' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' d' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // C 风格的字符串< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > char< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > c2< / 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;" > {< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' H' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' e' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' l' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' l' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' o' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' ' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' W' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' o' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' r' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' l' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' d' < / 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;" > \0< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ' < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < / div > < / div > < h3 id = "_4-2-2-简化写法-推荐" tabindex = "-1" > 4.2.2 简化写法(推荐) < a class = "header-anchor" href = "#_4-2-2-简化写法-推荐" aria-label = "Permalink to "4.2.2 简化写法(推荐)"" > < / a > < / h3 > < ul > < li > 字符串写成数组的形式, 非常麻烦。C 语言中提供了一种简化写法,即:双引号中的字符,会自动视为字符数组。< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 简化写法会自动在末尾添加 < code > \0< / code > 字符,强烈推荐使用。< / p > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< 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:#6A737D;--shiki-dark:#6A737D;" > // 禁用 stdout 缓冲区< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > setbuf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (stdout, < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > NULL< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > char< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > c1< / 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;" > {< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " Hello World" < / 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:#D73A49;--shiki-dark:#F97583;" > char< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > c2< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > []< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " Hello World" < / 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:#24292E;--shiki-dark:#E1E4E8;" > < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < / div > < / div > < h2 id = "_4-3-字符串的输入和输出" tabindex = "-1" > 4.3 字符串的输入和输出 < a class = "header-anchor" href = "#_4-3-字符串的输入和输出" aria-label = "Permalink to "4.3 字符串的输入和输出"" > < / a > < / h2 > < ul > < li > 对于字符串的输入和输出,同样可以使用 < code > scanf< / code > 和 < code > printf< / code > 函数来实现,并且其格式占位符是 < code > %s< / code > 。< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < p > 之前提到,对于 scanf 函数而言,< code > %s< / code > 默认是匹配到空格或 Enter 键,如果我们输入的字符串是 < code > Hello World< / code > ,就只能得到 < code > Hello< / code > ;如果要实现匹配到换行,则可以在输入的时候,将格式占位符 < code > %s< / code > 替换为 < code > %[^\n]< / code > 。< / p > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< 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:#6A737D;--shiki-dark:#6A737D;" > // 禁用 stdout 缓冲区< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > setbuf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (stdout, < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > NULL< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > char< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > c1< / 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;" > {< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " Hello World" < / 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:#D73A49;--shiki-dark:#F97583;" > char< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > c2< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > []< / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > =< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " Hello World" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // //可以省略一对 {} 来初始化数组元素< / 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;" > " c1 = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %s\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , c1);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // c1 = Hello World< / 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;" > " c2 = < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %s\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , c2);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // c2 = Hello World< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < / div > < / div > < ul > < li > 示例:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< 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:#6A737D;--shiki-dark:#6A737D;" > // 禁用 stdout 缓冲区< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > setbuf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (stdout, < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > NULL< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > char< / span > < span style = "--shiki-light:#E36209;--shiki-dark:#FFAB70;" > str< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > [< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 32< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ];< / 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;" > " 请输入字符串:" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > );< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > scanf< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > (< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#B31D28;--shiki-dark:#FDAEB7;--shiki-light-font-style:italic;--shiki-dark-font-style:italic;" > %< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > [^< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > ]" < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , str);< / 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;" > " 字符串是:< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %s\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , str);< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-08-11 01:49:02 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < span class = "line-number" > 15< / span > < br > < / div > < / div > < 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 > < h3 id = "_5-1-1-内存" tabindex = "-1" > 5.1.1 内存 < a class = "header-anchor" href = "#_5-1-1-内存" aria-label = "Permalink to "5.1.1 内存"" > < / a > < / h3 > < ul > < li > < p > < code > 内存< / code > 是一种< code > 计算机硬件< / code > ,是< code > 软件< / code > 在< code > 运行过程< / code > 中,用来< code > 临时存储数据< / code > 的。在生活中,最为常见的< code > 内存< / code > 就是< code > 随机存取存储器( RAM, 内存条< / code > ),其特点如下所示:< / p > < ul > < li > ① 生活中最常见的内存类型,用于存储当前运行的程序和数据。< / li > < li > ② 内存是易失性存储器,这意味着断电后数据会丢失。< / li > < li > ③ 它具有高速读写特性,适用于需要快速访问的操作。< / li > < / ul > < / li > < li > < p > 内存条的外观,如下所示:< / p > < / li > < / ul > < p > < img src = "/c/assets/24.QdyrLkbx.jpeg" alt = "" > < / p > < ul > < li > 像我们平常使用< code > 记事本< / code > 软件一样,当我们输入一些文字的时候,其实是将数据< code > 临时< / code > 保存在内存中的,如下所示:< / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① 目前,很多软件都很智能,如果用户没有将数据到保存文件中,将显示红色,以警告用户还没有保存数据,提醒用户需要尽快保存数据!!!< / li > < li > ② 但是, 也有很多软件提供了自动保存数据的功能, 其原理就是定时( 1s、3s、5s) 将内存中的数据刷新到文件中, 以防止数据丢失! ! ! < / li > < li > ③ 将数据从内存存储到文件中,专业的说法是落盘(落在磁盘上)。< / li > < / ul > < / div > < p > < img src = "/c/assets/25.wg7a3XFP.gif" alt = "" > < / p > < ul > < li > 此时,如果我们在没有保存的过程下,将< code > 记事本< / code > 软件关闭,那么刚才输入的文字将丢失;下次,再打开同样的文件(将数据从磁盘加载进内存,再交给 CPU) , 之前输入的文字将不复存在, 如下所示: < / li > < / ul > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① 目前,很多软件都很智能,如果你没有保存,将提醒你是否保存或丢失刚才输入的文字。< / li > < li > ② 但是, 也有很多软件提供了自动保存数据的功能, 其原理就是定时( 1s、3s、5s) 将内存中的数据刷新到文件中, 以防止数据丢失! ! ! < / li > < li > ③ 将数据从内存存储到文件中,专业的说法是落盘(落在磁盘上)。< / li > < / ul > < / div > < p > < img src = "/c/assets/26.M8jrrq2u.gif" alt = "" > < / p > < div class = "important custom-block github-alert" > < p class = "custom-block-title" > IMPORTANT< / p > < p > < / p > < p > 内存就是软件在运行过程中,用来临时存储数据的,最为重要的两个步骤就是:< / p > < ul > < li > ① 将数据< code > 保存< / code > 到内存中。< / li > < li > ② 从内存中的<
2024-08-07 09:14:37 +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:#24292E;--shiki-dark:#E1E4E8;" > < / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义一个变量并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > 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:#24292E;--shiki-dark:#E1E4E8;" > < / 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 > < span class = "line-number" > 9< / span > < br > < / div > < / div > < ul > < li > 那么,编译器就会这么处理,如下所示:< / li > < / ul > < p > < img src = "/c/assets/37.EU7nYUIS.svg" alt = "" > < / p > < ul > < li > 在代码中,我们可以使用 < code > & 变量名< / code > 来获取一个变量的内存首地址,如下所示:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义一个变量并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > 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 >
< 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;" > " 变量 a 的首地址是: < / 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:#24292E;--shiki-dark:#E1E4E8;" > a);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 变量 a 的首地址是: 0000002bf1dffd0c< / 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;" > " 变量 a 的中保存的值是: < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %d\n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , a);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 变量 a 的中保存的值是: 10< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-08-11 09:02:51 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < / div > < / div > < div class = "note custom-block github-alert" > < p class = "custom-block-title" > NOTE< / p > < p > < / p > < ul > < li > ① < code > 变量< / code > 是对程序中< code > 数据< / code > 在内存中存储空间的抽象,如果不涉及到指针变量,那我们在编码的时候,就只需要将变量等价于内存中存储空间里面存储的数据,而不需要再去考虑编译器底层是如何转换,提高了开发效率(机器语言和汇编可不是这样的,需要关注每个细节)。< / li > < li > ② 数据类型只在< code > 定义< / code > 变量的时候< code > 指定< / code > ,而且必须指定;< code > 使用< / code > 变量的时候< code > 无需< / code > 再声明,因为此时的数据类型已经确定了。< / li > < / ul > < / div > < h2 id = "_5-3-内存中的数组" tabindex = "-1" > 5.3 内存中的数组 < a class = "header-anchor" href = "#_5-3-内存中的数组" aria-label = "Permalink to "5.3 内存中的数组"" > < / a > < / h2 > < ul > < li > 如果我们在代码中这么定义数组,如下所示:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
2024-08-07 09:14:37 +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:#24292E;--shiki-dark:#E1E4E8;" > < / span > < / span >
< span class = "line" > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // 定义一个数组并初始化< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ,< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / span > < / span >
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > < / span > < / span >
< span class = "line" > < span style = "--shiki-light:#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 > < span class = "line-number" > 9< / span > < br > < / div > < / div > < ul > < li > 那么,编译器就会这么处理,如下所示:< / li > < / ul > < p > < img src = "/c/assets/38.BMoE_Ep0.svg" alt = "" > < / p > < ul > < li > 在代码中,我们可以直接打印数组名的内存地址,如下所示:< / li > < / ul > < div class = "language-c vp-adaptive-theme line-numbers-mode" > < button title = "Copy Code" class = "copy" > < / button > < span class = "lang" > c< / span > < pre class = "shiki shiki-themes github-light github-dark vp-code" tabindex = "0" > < code > < span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > #include< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > < stdio.h> < / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#6F42C1;--shiki-dark:#B392F0;" > main< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > () {< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > int< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr< / 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;" > {< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 3< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > };< / 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;" > " arr 的首地址是: < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \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;" > // arr 的首地址是: 0000003a6f7ffcd4< / 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;" > " arr 的首地址是: < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \n< / span > < span style = "--shiki-light:#032F62;--shiki-dark:#9ECBFF;" > " < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > , < / span > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > & < / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > arr);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // & arr 的地址是: 0000003a6f7ffcd4< / 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;" > " arr[0] 的地址是: < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \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;" > // arr[0] 的地址是: 0000003a6f7ffcd4< / 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;" > " arr[1] 的地址是: < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \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;" > 1< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[1] 的地址是: 0000003a6f7ffcd8< / 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;" > " arr[2] 的地址是: < / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > %p< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > \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;" > 2< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ]);< / span > < span style = "--shiki-light:#6A737D;--shiki-dark:#6A737D;" > // arr[2] 的地址是: 0000003a6f7ffcdc< / span > < / span >
< span class = "line" > < / span >
< span class = "line" > < span style = "--shiki-light:#D73A49;--shiki-dark:#F97583;" > return< / span > < span style = "--shiki-light:#005CC5;--shiki-dark:#79B8FF;" > 0< / span > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > ;< / span > < / span >
2024-08-11 09:02:51 +02:00
< span class = "line" > < span style = "--shiki-light:#24292E;--shiki-dark:#E1E4E8;" > }< / span > < / span > < / code > < / pre > < div class = "line-numbers-wrapper" aria-hidden = "true" > < span class = "line-number" > 1< / span > < br > < span class = "line-number" > 2< / span > < br > < span class = "line-number" > 3< / span > < br > < span class = "line-number" > 4< / span > < br > < span class = "line-number" > 5< / span > < br > < span class = "line-number" > 6< / span > < br > < span class = "line-number" > 7< / span > < br > < span class = "line-number" > 8< / span > < br > < span class = "line-number" > 9< / span > < br > < span class = "line-number" > 10< / span > < br > < span class = "line-number" > 11< / span > < br > < span class = "line-number" > 12< / span > < br > < span class = "line-number" > 13< / span > < br > < span class = "line-number" > 14< / span > < br > < / div > < / div > < div class = "warning custom-block github-alert" > < p class = "custom-block-title" > WARNING< / p > < p > < / p > < p > 在上述示例中,< code > arr< / code > 和 < code > & arr< / code > 的值是一样的,但是对应的含义是不同的。< / p > < ul > < li > ① < code > arr< / code > 是数组名,在大多数情况下会转换为数组第一个元素的地址,即:< code > arr< / code > 等价于 < code > & arr[0]< / code > ,其数据类型是 < code > int *< / code > 。< / li > < li > ② < code > & arr< / code > 是数组名的地址,即整个数组的地址,它指向数组本身,并不是数组第一个元素的地址,< code > & arr< / code > 的数据类型是 < code > int(*)[3]< / code > 。< / li > < / ul > < / div > < / 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-11T07:02:16.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/04_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/06_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-12 02:36:26 +02:00
< script > w i n d o w . _ _ V P _ H A S H _ M A P _ _ = J S O N . p a r s e ( " { \ " i n d e x . m d \ " : \ " n 0 W - F d - u \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 0 _ x d x _ i n d e x . m d \ " : \ " B U 3 n 8 p J S \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 1 _ x d x _ i n d e x . m d \ " : \ " b 9 A a D Z D w \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 2 _ x d x _ i n d e x . m d \ " : \ " 0 G H s M M a 0 \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 3 _ x d x _ i n d e x . m d \ " : \ " C C W _ w i D C \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 4 _ x d x _ i n d e x . m d \ " : \ " D k X f R s s o \ " , \ " n o t e s _ 0 1 _ c - b a s i c _ 0 5 _ x d x _ i n d e x . m d \ " : \ " C B i i Y 4 - B \ " , \ " 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 8 Z 5 K X t i \ " , \ " n o t e s _ 0 4 _ l i n u x _ 0 1 _ x d x _ i n d e x . m d \ " : \ " D 7 - U l H S H \ " , \ " n o t e s _ i n d e x . m d \ " : \ " b g N n s 6 d 1 \ " } " ) ; w i n d o w . _ _ V P _ S I T E _ D A T A _ _ = J S O N . p a r s e ( " { \ " l a n g \ " : \ " z h - C N \ " , \ " d i r \ " : \ " l t r \ " , \ " t i t l e \ " : \ " 许 大 仙 \ " , \ " t i t l e T e m p l a t e \ " : \ " H i , 终 于 等 到 你 \ " , \ " d e s c r i p t i o n \ " : \ " 许 大 仙 前 端 、 J a v a 、 大 数 据 、 云 原 生 \ " , \ " b a s e \ " : \ " / c / \ " , \ " h e a d \ " : [ ] , \ " r o u t e r \ " : { \ " p r e f e t c h L i n k s \ " : t r u e } , \ " a p p e a r a n c e \ " : t r u e , \ " t h e m e C o n f i g \ " : { \ " l a s t U p d a t e d T e x t \ " : \ " 上 次 更 新 \ " , \ " r e t u r n T o T o p L a b e l \ " : \ " 返 回 顶 部 \ " , \ " s e a r c h \ " : { \ " p r o v i d e r \ " : \ " l o c a l \ " } , \ " l o g o \ " : \ " / l o g o . s v g \ " , \ " n a v \ " : [ { \ " t e x t \ " : \ " 首 页 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / \ " } , { \ " t e x t \ " : \ " 计 组 6 件 套 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / c o a 6 / n o t e s / \ " } , { \ " t e x t \ " : \ " 编 程 语 言 \ " , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " c / c + + \ " , \ " l i n k \ " : \ " / n o t e s / \ " } , { \ " t e x t \ " : \ " 前 端 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / w e b - d e s i g n / n o t e s / \ " } , { \ " t e x t \ " : \ " J a v a \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / j a v a / n o t e s / \ " } , { \ " t e x t \ " : \ " 大 数 据 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / b i g - d a t a / n o t e s / \ " } , { \ " t e x t \ " : \ " 云 原 生 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / l i n u x / n o t e s / \ " } , { \ " t e x t \ " : \ " G o \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / g o / n o t e s / \ " } , { \ " t e x t \ " : \ " P y t h o n \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / p y t h o n / n o t e s / \ " } ] } , { \ " t e x t \ " : \ " 开 源 软 件 \ " , \ " l i n k \ " : \ " h t t p s : / / a e x i a r . g i t h u b . i o / o p e n - s o f t w a r e / n o t e s / \ " } ] , \ " s i d e b a r \ " : { \ " / n o t e s / \ " : [ { \ " t e x t \ " : \ " C 语 言 基 础 \ " , \ " c o l l a p s e d \ " : t r u e , \ " i t e m s \ " : [ { \ " t e x t \ " : \ " 编 程 基 础 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 0 _ x d x / \ " } , { \ " t e x t \ " : \ " C 语 言 入 门 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 1 _ x d x / \ " } , { \ " t e x t \ " : \ " 变 量 和 进 制 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 2 _ x d x / \ " } , { \ " t e x t \ " : \ " 数 据 类 型 和 运 算 符 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 3 _ x d x / \ " } , { \ " t e x t \ " : \ " 流 程 控 制 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 4 _ x d x / \ " } , { \ " t e x t \ " : \ " 数 组 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 5 _ x d x / \ " } , { \ " t e x t \ " : \ " 指 针 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 6 _ x d x / \ " } , { \ " t e x t \ " : \ " 函 数 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 7 _ x d x / \ " } , { \ " t e x t \ " : \ " 预 处 理 器 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 8 _ x d x / \ " } , { \ " t e x t \ " : \ " 自 定 义 数 据 类 型 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 0 9 _ x d x / \ " } , { \ " t e x t \ " : \ " 字 符 串 和 时 间 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 1 0 _ x d x / \ " } , { \ " t e x t \ " : \ " 内 存 管 理 \ " , \ " l i n k \ " : \ " / n o t e s / 0 1 _ c - b a s i c / 1 1 _ x d x / \ " } , { \ " t e x t \ " : \ " 文 件 操 作 \ " , \ " 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
2024-07-24 09:39:31 +02:00
< / body >
< / html >