news 2026/6/15 15:17:52

指针的基本概念 指针是C语言中存储变量内存地址的变量

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
指针的基本概念 指针是C语言中存储变量内存地址的变量

指针的基本概念

指针是C语言中存储变量内存地址的变量。通过指针可以直接访问或修改内存中的数据,常用于动态内存分配、数组操作和函数参数传递。

指针变量声明需使用*符号,例如int *p;表示p是一个指向整型数据的指针。指针的值是另一个变量的地址,可通过&运算符获取变量的地址。

int a = 10; int *p = &a; // p指向变量a的地址

指针的初始化和使用

未初始化的指针称为“野指针”,可能指向无效内存,导致程序崩溃。指针初始化时应明确指向有效地址或设为NULL

int *p = NULL; // 初始化为空指针 if (p != NULL) { *p = 20; // 安全操作:检查指针非空后再解引用 }

通过*运算符可解引用指针,访问或修改其指向的值:

int a = 10; int *p = &a; printf("%d", *p); // 输出10 *p = 20; // 修改a的值为20

指针与数组的关系

数组名本质是首元素的地址,指针可遍历数组:

int arr[3] = {1, 2, 3}; int *p = arr; // p指向数组首地址 printf("%d", *(p + 1)); // 输出第二个元素2

指针算术运算允许通过加减整数移动指针位置,例如p + 1指向下一个元素(实际地址增加sizeof(类型)字节)。

指针作为函数参数

指针传递允许函数修改实参的值,避免数据拷贝:

void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int main() { int a = 1, b = 2; swap(&a, &b); // 传递地址 printf("%d %d", a, b); // 输出2 1 }

动态内存分配

使用mallocfree实现动态内存管理:

int *p = (int *)malloc(5 * sizeof(int)); // 分配5个整型空间 if (p != NULL) { p[0] = 10; // 通过指针使用内存 free(p); // 释放内存 }

常见错误与注意事项

  • 空指针解引用:访问NULL指针会导致段错误。
  • 内存泄漏:动态分配的内存未释放。
  • 指针类型匹配:不同类型的指针赋值需强制类型转换。

指针是C语言的核心特性,需通过实践逐步掌握其灵活性与风险。

https://avg.163.com/topic/detail/8664476
https://avg.163.com/topic/detail/8664442
https://avg.163.com/topic/detail/8664453
https://avg.163.com/topic/detail/8664484
https://avg.163.com/topic/detail/8664465
https://avg.163.com/topic/detail/8664473
https://avg.163.com/topic/detail/8664486
https://avg.163.com/topic/detail/8664461
https://avg.163.com/topic/detail/8664436
https://avg.163.com/topic/detail/8664447
https://avg.163.com/topic/detail/8664448
https://avg.163.com/topic/detail/8664485
https://avg.163.com/topic/detail/8664457
https://avg.163.com/topic/detail/8664439
https://avg.163.com/topic/detail/8664450
https://avg.163.com/topic/detail/8664462
https://avg.163.com/topic/detail/8664474
https://avg.163.com/topic/detail/8664483
https://avg.163.com/topic/detail/8664472
https://avg.163.com/topic/detail/8664482
https://avg.163.com/topic/detail/8664469
https://avg.163.com/topic/detail/8664480
https://avg.163.com/topic/detail/8664459
https://avg.163.com/topic/detail/8664470
https://avg.163.com/topic/detail/8664481
https://avg.163.com/topic/detail/8664435
https://avg.163.com/topic/detail/8664446
https://avg.163.com/topic/detail/8664458
https://avg.163.com/topic/detail/8664468
https://avg.163.com/topic/detail/8664479
https://avg.163.com/topic/detail/8664434
https://avg.163.com/topic/detail/8664445
https://avg.163.com/topic/detail/8664456
https://avg.163.com/topic/detail/8664467
https://avg.163.com/topic/detail/8664478
https://avg.163.com/topic/detail/8664219
https://avg.163.com/topic/detail/8664227
https://avg.163.com/topic/detail/8664234
https://avg.163.com/topic/detail/8664241
https://avg.163.com/topic/detail/8664249
https://avg.163.com/topic/detail/8664253
https://avg.163.com/topic/detail/8664262
https://avg.163.com/topic/detail/8664217
https://avg.163.com/topic/detail/8664225
https://avg.163.com/topic/detail/8664232
https://avg.163.com/topic/detail/8664270
https://avg.163.com/topic/detail/8664220
https://avg.163.com/topic/detail/8664277
https://avg.163.com/topic/detail/8664284
https://avg.163.com/topic/detail/8664298
https://avg.163.com/topic/detail/8664215
https://avg.163.com/topic/detail/8664222
https://avg.163.com/topic/detail/8664229
https://avg.163.com/topic/detail/8664237
https://avg.163.com/topic/detail/8664244
https://avg.163.com/topic/detail/8664305
https://avg.163.com/topic/detail/8664239
https://avg.163.com/topic/detail/8664246
https://avg.163.com/topic/detail/8664228
https://avg.163.com/topic/detail/8664235
https://avg.163.com/topic/detail/8664242
https://avg.163.com/topic/detail/8664248
https://avg.163.com/topic/detail/8664257
https://avg.163.com/topic/detail/8664260
https://avg.163.com/topic/detail/8664269
https://avg.163.com/topic/detail/8664276
https://avg.163.com/topic/detail/8664216
https://avg.163.com/topic/detail/8664251
https://avg.163.com/topic/detail/8664312
https://avg.163.com/topic/detail/8664223
https://avg.163.com/topic/detail/8664256
https://avg.163.com/topic/detail/8664261
https://avg.163.com/topic/detail/8664267
https://avg.163.com/topic/detail/8664274
https://avg.163.com/topic/detail/8664281
https://avg.163.com/topic/detail/8664288
https://avg.163.com/topic/detail/8664218
https://avg.163.com/topic/detail/8664295
https://avg.163.com/topic/detail/8664226
https://avg.163.com/topic/detail/8664283
https://avg.163.com/topic/detail/8664258
https://avg.163.com/topic/detail/8664319
https://avg.163.com/topic/detail/8664290
https://avg.163.com/topic/detail/8664230
https://avg.163.com/topic/detail/8664221
https://avg.163.com/topic/detail/8664224
https://avg.163.com/topic/detail/8664238
https://avg.163.com/topic/detail/8664231
https://avg.163.com/topic/detail/8664243
https://avg.163.com/topic/detail/8664250
https://avg.163.com/topic/detail/8664255
https://avg.163.com/topic/detail/8664302
https://avg.163.com/topic/detail/8664265
https://avg.163.com/topic/detail/8664233
https://avg.163.com/topic/detail/8664309
https://avg.163.com/topic/detail/8664327
https://avg.163.com/topic/detail/8664297
https://avg.163.com/topic/detail/8664304
https://avg.163.com/topic/detail/8664311
https://avg.163.com/topic/detail/8664318
https://avg.163.com/topic/detail/8664245
https://avg.163.com/topic/detail/8664252
https://avg.163.com/topic/detail/8664259
https://avg.163.com/topic/detail/8664266
https://avg.163.com/topic/detail/8664264
https://avg.163.com/topic/detail/8664272
https://avg.163.com/topic/detail/8664271
https://avg.163.com/topic/detail/8664240
https://avg.163.com/topic/detail/8664316
https://avg.163.com/topic/detail/8664334
https://avg.163.com/topic/detail/8664338
https://avg.163.com/topic/detail/8664348
https://avg.163.com/topic/detail/8664325
https://avg.163.com/topic/detail/8664324
https://avg.163.com/topic/detail/8664333
https://avg.163.com/topic/detail/8664342
https://avg.163.com/topic/detail/8664273
https://avg.163.com/topic/detail/8664347
https://avg.163.com/topic/detail/8664278
https://avg.163.com/topic/detail/8664279
https://avg.163.com/topic/detail/8664247
https://avg.163.com/topic/detail/8664285
https://avg.163.com/topic/detail/8664292
https://avg.163.com/topic/detail/8664356
https://avg.163.com/topic/detail/8664330
https://avg.163.com/topic/detail/8664299
https://avg.163.com/topic/detail/8664254
https://avg.163.com/topic/detail/8664263
https://avg.163.com/topic/detail/8664268
https://avg.163.com/topic/detail/8664280
https://avg.163.com/topic/detail/8664286
https://avg.163.com/topic/detail/8664354
https://avg.163.com/topic/detail/8664287
https://avg.163.com/topic/detail/8664294
https://avg.163.com/topic/detail/8664301
https://avg.163.com/topic/detail/8664360
https://avg.163.com/topic/detail/8664337
https://avg.163.com/topic/detail/8664308
https://avg.163.com/topic/detail/8664306
https://avg.163.com/topic/detail/8664313
https://avg.163.com/topic/detail/8664320
https://avg.163.com/topic/detail/8664275
https://avg.163.com/topic/detail/8664282
https://avg.163.com/topic/detail/8664361
https://avg.163.com/topic/detail/8664293
https://avg.163.com/topic/detail/8664289
https://avg.163.com/topic/detail/8664296
https://avg.163.com/topic/detail/8664303
https://avg.163.com/topic/detail/8664368
https://avg.163.com/topic/detail/8664310
https://avg.163.com/topic/detail/8664345
https://avg.163.com/topic/detail/8664315
https://avg.163.com/topic/detail/8664323
https://avg.163.com/topic/detail/8664329
https://avg.163.com/topic/detail/8664326
https://avg.163.com/topic/detail/8664332
https://avg.163.com/topic/detail/8664366
https://avg.163.com/topic/detail/8664300
https://avg.163.com/topic/detail/8664340
https://avg.163.com/topic/detail/8664349
https://avg.163.com/topic/detail/8664352
https://avg.163.com/topic/detail/8664374
https://avg.163.com/topic/detail/8664353
https://avg.163.com/topic/detail/8664317
https://avg.163.com/topic/detail/8664363
https://avg.163.com/topic/detail/8664370
https://avg.163.com/topic/detail/8664339
https://avg.163.com/topic/detail/8664344
https://avg.163.com/topic/detail/8664351
https://avg.163.com/topic/detail/8664359
https://avg.163.com/topic/detail/8664375
https://avg.163.com/topic/detail/8664307
https://avg.163.com/topic/detail/8664314
https://avg.163.com/topic/detail/8664321
https://avg.163.com/topic/detail/8664382
https://avg.163.com/topic/detail/8664358
https://avg.163.com/topic/detail/8664322
https://avg.163.com/topic/detail/8664377
https://avg.163.com/topic/detail/8664331
https://avg.163.com/topic/detail/8664328
https://avg.163.com/topic/detail/8664336
https://avg.163.com/topic/detail/8664367
https://avg.163.com/topic/detail/8664381
https://avg.163.com/topic/detail/8664343
https://avg.163.com/topic/detail/8664350
https://avg.163.com/topic/detail/8664389
https://avg.163.com/topic/detail/8664357
https://avg.163.com/topic/detail/8664365
https://avg.163.com/topic/detail/8664384
https://avg.163.com/topic/detail/8664373
https://avg.163.com/topic/detail/8664341
https://avg.163.com/topic/detail/8664346
https://avg.163.com/topic/detail/8664372
https://avg.163.com/topic/detail/8664355
https://avg.163.com/topic/detail/8664390
https://avg.163.com/topic/detail/8664399
https://avg.163.com/topic/detail/8664395
https://avg.163.com/topic/detail/8664403
https://avg.163.com/topic/detail/8664364
https://avg.163.com/topic/detail/8664392
https://avg.163.com/topic/detail/8664371
https://avg.163.com/topic/detail/8664380
https://avg.163.com/topic/detail/8664388
https://avg.163.com/topic/detail/8664396
https://avg.163.com/topic/detail/8664401
https://avg.163.com/topic/detail/8664379
https://avg.163.com/topic/detail/8664362
https://avg.163.com/topic/detail/8664369
https://avg.163.com/topic/detail/8664376
https://avg.163.com/topic/detail/8664404
https://avg.163.com/topic/detail/8664410
https://avg.163.com/topic/detail/8664411
https://avg.163.com/topic/detail/8664398
https://avg.163.com/topic/detail/8664378
https://avg.163.com/topic/detail/8664406
https://avg.163.com/topic/detail/8664413
https://avg.163.com/topic/detail/8664409
https://avg.163.com/topic/detail/8664387
https://avg.163.com/topic/detail/8664420
https://avg.163.com/topic/detail/8664427
https://avg.163.com/topic/detail/8664383
https://avg.163.com/topic/detail/8664417
https://avg.163.com/topic/detail/8664391
https://avg.163.com/topic/detail/8664418
https://avg.163.com/topic/detail/8664423
https://avg.163.com/topic/detail/8664386
https://avg.163.com/topic/detail/8664393

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/14 22:08:16

M2FP模型在智慧酒店中的服务优化应用

M2FP模型在智慧酒店中的服务优化应用 🌐 智慧酒店场景下的AI视觉新范式 随着智能硬件与边缘计算的快速发展,智慧酒店正从“自动化”迈向“智能化”。传统的人体检测或行为识别系统多停留在“是否有人”、“动作分类”的粗粒度层面,难以支撑精…

作者头像 李华
网站建设 2026/6/15 9:44:18

WeClone:用AI创造你的专属数字分身,从此拥有24小时在线助手

WeClone:用AI创造你的专属数字分身,从此拥有24小时在线助手 【免费下载链接】WeClone 欢迎star⭐。使用微信聊天记录微调大语言模型,并绑定到微信机器人,实现自己的数字克隆。 数字克隆/数字分身/LLM/大语言模型/微信聊天机器人/L…

作者头像 李华
网站建设 2026/6/15 9:43:52

游戏素材提取终极指南:从入门到精通的技术实践

游戏素材提取终极指南:从入门到精通的技术实践 【免费下载链接】game-hacking 项目地址: https://gitcode.com/gh_mirrors/ga/game-hacking 游戏素材提取是游戏逆向工程中的核心技术环节,无论是制作游戏MOD、学习游戏开发技巧,还是进…

作者头像 李华
网站建设 2026/6/15 9:44:18

如何用M2FP实现智能摄影灯光调节?

如何用M2FP实现智能摄影灯光调节? 🌟 引言:从人体解析到智能光影控制 在现代智能摄影系统中,精准的灯光调节是提升成像质量的关键环节。传统自动曝光和白平衡算法往往基于全局图像统计信息进行调整,难以针对人物关键部…

作者头像 李华
网站建设 2026/6/5 9:35:00

自主机器人入门指南:从零基础到实践专家的完整学习路径

自主机器人入门指南:从零基础到实践专家的完整学习路径 【免费下载链接】Introduction-to-Autonomous-Robots Introduction to Autonomous Robots 项目地址: https://gitcode.com/gh_mirrors/in/Introduction-to-Autonomous-Robots 想要深入了解自主机器人的…

作者头像 李华
网站建设 2026/6/15 0:40:57

WorldGuard插件完整教程:构建安全的Minecraft服务器环境

WorldGuard插件完整教程:构建安全的Minecraft服务器环境 【免费下载链接】WorldGuard 🛡️ Protect your Minecraft server and lets players claim areas 项目地址: https://gitcode.com/gh_mirrors/wo/WorldGuard WorldGuard插件是Minecraft服务…

作者头像 李华