> For the complete documentation index, see [llms.txt](https://labspc.gitbook.io/cnippets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://labspc.gitbook.io/cnippets/chap5.-zhi-zhen-yu-shu-zu/5.1-zhi-zhen-yu-di-zhi.md).

# 5.1 指针与地址

厦门大学的视频：

【C语言程序设计第8章：善于利用指针-哔哩哔哩】 <https://b23.tv/44aBZHa>

【C语言程序设计第8章：善于利用指针-哔哩哔哩】 <https://b23.tv/bn7lpdi>

### 5.1.1 什么是指针 <a href="#ks6wo" id="ks6wo"></a>

<img src="https://cdn.nlark.com/yuque/0/2024/jpeg/1171985/1705937683555-11e5d196-d782-4bcb-9f53-6bcb18f92ea8.jpeg" alt="" width="2360">

<img src="https://cdn.nlark.com/yuque/0/2023/png/1171985/1694337760429-026416cb-a3ca-4c2b-9fbc-0950f8b7f66c.png" alt="" width="406">

<img src="https://cdn.nlark.com/yuque/0/2023/png/1171985/1700196138245-d29895cf-c93b-4fa1-9136-3883c7549fc3.png" alt="" width="763">

<img src="https://cdn.nlark.com/yuque/0/2024/jpeg/1171985/1705938228981-c331f31b-66f3-42de-8672-ff6e9896fd05.jpeg" alt="" width="4030">

### 5.1.2 如何定义指针变量 <a href="#q4qv4" id="q4qv4"></a>

<img src="https://cdn.nlark.com/yuque/0/2024/png/1171985/1705938092598-d82d6852-474d-405c-bd41-408542f58e2c.png" alt="" width="1416">

<img src="https://cdn.nlark.com/yuque/0/2023/png/1171985/1694350587901-ac7a3788-495a-4c20-9056-42b3609dd325.png" alt="" width="615">

~~2024-3-24  再来看上图，突然看不懂，感觉有错误，又不是很确定。~~

笔记：这是取址 `&` 之后，直接赋值。这句代码将变量 `p` 初始化为指向变量 `a` 的地址。这意味着 `p` 指针现在存储了变量 `a` 的地址。

<img src="https://cdn.nlark.com/yuque/0/2024/jpeg/1171985/1706322908007-9a161ac0-2fc0-454b-97e6-fada829d9f89.jpeg" alt="" width="2360">

### 5.1.3 指针变量的大小 <a href="#kogi7" id="kogi7"></a>

**指针变量存的是什么？存的是一个地址，所以说你给指针变量指定了类型，但是根本不影响，它本身只是一个地址，它的大小和你这个指定的类型的大小是没有关系的。指针变量的大小和机器的位数是有关系的**， 指针即地址，32位机、64位机，就是说这个地址是32比特位、64个比特位，x64平台下，指针变量的大小，就是64个比特位，也就是8字节。

<img src="https://cdn.nlark.com/yuque/0/2023/png/1171985/1694354175702-40a80f95-168f-440d-a485-6c37961322c0.png" alt="" width="664">

<img src="https://cdn.nlark.com/yuque/0/2023/png/1171985/1694354139276-d2e2ee7e-af56-45a2-8af3-a96fc6217acc.png" alt="" width="718">

### 5.1.4 取址 & <a href="#ljbt8" id="ljbt8"></a>

<img src="https://cdn.nlark.com/yuque/0/2023/png/1171985/1700196529092-b7302504-124a-4b6d-b5ab-47e12fad01c3.png" alt="" width="785">

**取址，取的其实是开头的地址，就是开头的编号。**

```
// demo
#include <stdio.h>
int main() {
    int a = 10;
    printf("%p\n", &a);      //&a 取址
    return 0;
}
```

`&a` 取出来的是地址`0x16d833308`，也是个数值。

<img src="https://cdn.nlark.com/yuque/0/2023/png/1171985/1694337426327-193b3291-d15b-4ef8-b254-04aafd337191.png" alt="" width="792">

指针变量仅能存放某个地址，可以取址，但是**不能直接指定值**。

<img src="https://cdn.nlark.com/yuque/0/2024/jpeg/1171985/1706323466012-d773917d-db5b-4e18-b5d8-f553f71e7b90.jpeg" alt="" width="2360">

例外，可以初始化为 0，空指针，不指向任何东西。

### 5.1.5 寻址 \* <a href="#oen2q" id="oen2q"></a>

<img src="https://cdn.nlark.com/yuque/0/2024/png/1171985/1705940437362-32d512e7-215e-4d73-b139-5fcd760c43f6.png" alt="" width="2176">

```
#include <stdio.h>

int i;
int *p = &i;
int main()
{
    i = 10;
    printf("%d\n", i);
    printf("\n");
    printf("%d\n", *p);
    printf("\n");
    *p = 20;
    printf("%d\n", i);
    printf("\n");
    printf("%d\n", *p);
    return 0;
}
```

### 5.1.6 指针变量的初始化 <a href="#fgrb2" id="fgrb2"></a>

如果不初始化，指针变量的值是不确定的，所以指针变量的初始化很重要。初始化可以有两种：

```
int i;
int *p=&i;
```

```
int *p=NULL;
```

如果一开始不知道指针指向何处，最简单的方式就是将指针初始化为`NULL`，也就是置 0。

### 5.1.7 (& \*p) 和 (\*\&p) 的区别 <a href="#rvmwg" id="rvmwg"></a>

<img src="https://cdn.nlark.com/yuque/0/2024/jpeg/1171985/1706325247210-08aa33ad-0c04-4e7f-92e2-7bb112f34607.jpeg" alt="" width="2360">

注意：两种表示方式中，对 p 的要求不一样。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://labspc.gitbook.io/cnippets/chap5.-zhi-zhen-yu-shu-zu/5.1-zhi-zhen-yu-di-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
