网站开发 网络后台维护作用,网站建设实训总结,WordPress适合做电商吗,多个域名 一个网站数组与指针
数组: - 数组是一种数据结构#xff0c;可以存储固定大小的一组相同类型的元素。在内存中#xff0c;数组的元素是连续存储的。
指针: - 指针是一个变量#xff0c;用于存储内存地址。指针本身占用内存#xff0c;用来指向某个数据的地址。
数组与指针的关系…数组与指针
数组: - 数组是一种数据结构可以存储固定大小的一组相同类型的元素。在内存中数组的元素是连续存储的。
指针: - 指针是一个变量用于存储内存地址。指针本身占用内存用来指向某个数据的地址。
数组与指针的关系: - 数组名本质上是一个指向数组首元素的指针。比如int arr[5] 声明了一个包含5个整数的数组arr是指向arr[0]的指针。 #include stdio.h
int main() { int arr[5] {1, 2, 3, 4, 5}; int *arrPtr arr; // arrPtr指向arr的首元素 printf(Array elements using pointer:\n); for (int i 0; i 5; i) { printf(%d , *(arrPtr i)); // 等价于 arr[i] } printf(\n); return 0; } *解释: - arrPtr 是一个指向 arr 首元素的指针通过 arrPtr i 可以访问数组中第 i 个元素的地址*(arrPtr i) 解引用得到该元素的值。
字符串与指针
字符串: - 字符串是一个以 \0 结尾的字符数组。char str[] Hello; 和 char *str Hello; 是两种声明字符串的方式。
字符串与指针的关系: - 字符数组的首地址就是字符串的地址。可以使用指针遍历字符串。 #include stdio.h
int main() { char str[] Hello, World!; char *strPtr str; printf(String using pointer: %s\n, strPtr); return 0; } 解释: - str 是一个字符数组strPtr 是指向该数组首元素的指针。%s 格式化输出可以直接打印字符串。
指向函数的指针
函数指针: - 函数指针是指向函数的指针可以用来调用函数。函数指针的声明方式是返回类型 (*指针名)(参数类型列表); #include stdio.h
int multiply(int a, int b) { return a * b; }
int main() { int (*funcPtr)(int, int) multiply; // 函数指针funcPtr指向multiply函数 int result funcPtr(2, 3); // 使用函数指针调用multiply printf(Result of multiply using function pointer: %d\n, result); return 0; } 解释: - funcPtr 是一个函数指针指向 multiply 函数。通过 funcPtr(2, 3) 可以调用 multiply 函数并返回结果。
动态内存分配malloc
malloc函数: - malloc 用于在堆上分配指定字节数的内存并返回一个指向该内存的指针。分配的内存未初始化可能包含随机数据。 #include stdio.h #include stdlib.h
int main() { int *dynamicArray (int *)malloc(5 * sizeof(int)); // 分配5个int大小的内存 if (dynamicArray NULL) { printf(Memory allocation failed\n); return 1; // 内存分配失败退出程序 } // 初始化动态分配的数组 for (int i 0; i 5; i) { dynamicArray[i] i * i; // 存储0, 1, 4, 9, 16 } // 打印动态分配的数组 printf(Dynamically allocated array:\n); for (int i 0; i 5; i) { printf(%d , dynamicArray[i]); } printf(\n); // 释放动态分配的内存 free(dynamicArray); return 0; } 解释: - malloc(5 * sizeof(int)) 分配了存储5个整数的内存并返回一个指向该内存的指针。dynamicArray 指向这块内存。 - 检查 dynamicArray 是否为 NULL 是必要的因为内存分配可能失败。 - 使用 free(dynamicArray) 释放分配的内存以防止内存泄漏。
总结
- 数组与指针: 数组名是指向首元素的指针。可以通过指针访问数组元素。 - 字符串与指针: 字符数组名是字符串的地址。可以通过指针遍历和操作字符串。 - 函数指针: 函数指针可以指向函数并用来调用函数。 - 动态内存分配: malloc 分配动态内存free 释放内存。动态内存管理是C语言编程中的一个重要部分。 文章转载自: http://www.morning.kngqd.cn.gov.cn.kngqd.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.rhmpk.cn.gov.cn.rhmpk.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.ljjph.cn.gov.cn.ljjph.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.rgxn.cn.gov.cn.rgxn.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.drnfc.cn.gov.cn.drnfc.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.sskns.cn.gov.cn.sskns.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.cfynn.cn.gov.cn.cfynn.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.rmtxp.cn.gov.cn.rmtxp.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.crtgd.cn.gov.cn.crtgd.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.dmlgq.cn.gov.cn.dmlgq.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn