> For the complete documentation index, see [llms.txt](https://mqjyl2012.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mqjyl2012.gitbook.io/algorithm/c-cpp/c++-syntax/pointers-and-references.md).

# 指针 & 引用

## :pencil2: 1、指针

## :pencil2: 2、引用

### :pen\_fountain: 2.1、引用和指针的区别

1. 指针是一个实体，需要分配内存空间。引用只是变量的别名，不需要分配 内存空间；
2. 引用在定义的时候必须进行初始化，并且不能够改变。指针在定义的时候 不一定要初始化，并且指向的空间可变（注：不能有引用的值不能为 NULL）；
3. 有多级指针，但是没有多级引用，只能有一级引用；&#x20;
4. 指针和引用的自增运算结果不一样。（指针是指向下一个空间，引用时引 用的变量值加 1）；&#x20;
5. sizeof 引用得到的是所指向的变量（对象）的大小，而 sizeof 指针得 到的是指针本身的大小；
6. 引用访问一个变量是直接访问，而指针访问一个变量是间接访问；
7. 使用指针前最好做类型检查，防止野指针的出现；&#x20;
8. 引用底层是通过指针实现的；&#x20;
9. 作为参数时也不同，传指针的实质是传值，传递的值是指针的地址；传引 用的实质是传地址，传递的是变量的地址。

### :pen\_fountain: 2.2、左值引用和右值引用


---

# 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, and the optional `goal` query parameter:

```
GET https://mqjyl2012.gitbook.io/algorithm/c-cpp/c++-syntax/pointers-and-references.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
