Pointer, a weird beast, suitable for beginners and beyond
For beginners in C/C programming, pointers are one of the most difficult concepts to understand. Many students are troubled by this, and many developers try to avoid using pointers. However, understanding pointers is crucial, so let's start with the basics.
What is a pointer?
A pointer is a data type, similar to int
, float
, etc. The syntax for declaring a pointer is to add *
after the type name. For example, a pointer to an integer is declared as int *
, and a pointer to a custom structure mytype
is declared as mytype *
.
The pointer points to the memory address of the variable. int *
Storing the address of integer variables, mytype *
Storing the address of mytype
variables.
Example:
<code class="c ">#include<iostream> int main(){ int value = 42; int* p = &value; // p指向value的内存地址std::cout </iostream></code>
Main pointer operator
In addition to storing the address of a variable, pointers have two main operations: dereference and pointer arithmetic.
Decision
Use the *
operator to dereference the pointer to access the value in the memory unit pointed to by the pointer.
<code class="c ">#include<iostream> int main(){ int var = 42; int *p = &var; // p指向var的内存地址*p = 21; // 修改var的值为21 }</iostream></code>
An uninitialized pointer is very dangerous because it can point to anywhere in memory, causing the program to crash or undefined behavior.
Pointer arithmetic
Pointer arithmetic allows modifying the value of a pointer (i.e., memory address). Pointers can be added and subtracted, or added and subtracted with integers. It should be noted that the step size of the pointer addition and subtraction operation is the byte size of the data type pointed to. For example, if p
is a pointer to an integer, p
will make p
point to the address of the next integer.
Pointer arithmetic needs to be used with caution, as it allows direct operation of memory.
<code class="c ">#include<iostream> int main(){ int v = 21; int w = 42; int *p = &w; // p指向w p ; // p指向v *p = 1; // v的值加1 std::cout </iostream></code>
As shown in the example, we can modify the value of a variable that is not directly assigned through pointer arithmetic.
Empty pointer
Similar to the special position of the number 0 in numbers, nullpointer ( nullptr
) also plays an important role in pointers. A null pointer means that the pointer does not point to any valid memory address. Before using a pointer, be sure to check whether it is empty to avoid program crashes or errors.
<code class="c ">#include<iostream> int* foo(int value){ static int answ = 42; if(answ == value){ return &answ; } else { return nullptr; } } int main(){ int *p = foo(21); if (p != nullptr) { // 使用p } }</iostream></code>
Key points
To sum up, we learned:
- A pointer is a variable that stores memory addresses.
- Always initialize the pointer, uninitialized pointer may cause the program to crash.
- Always check if it is empty before dereference to the pointer.
- Use pointer arithmetic with caution.
Hope this guide will help you better understand pointers in C/C. Continue to learn and explore more!
The above is the detailed content of Pointer, a weird beast, suitable for beginners and beyond. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The CentOS shutdown command is shutdown, and the syntax is shutdown [Options] Time [Information]. Options include: -h Stop the system immediately; -P Turn off the power after shutdown; -r restart; -t Waiting time. Times can be specified as immediate (now), minutes ( minutes), or a specific time (hh:mm). Added information can be displayed in system messages.

Golang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.

Backup and Recovery Policy of GitLab under CentOS System In order to ensure data security and recoverability, GitLab on CentOS provides a variety of backup methods. This article will introduce several common backup methods, configuration parameters and recovery processes in detail to help you establish a complete GitLab backup and recovery strategy. 1. Manual backup Use the gitlab-rakegitlab:backup:create command to execute manual backup. This command backs up key information such as GitLab repository, database, users, user groups, keys, and permissions. The default backup file is stored in the /var/opt/gitlab/backups directory. You can modify /etc/gitlab

Enable PyTorch GPU acceleration on CentOS system requires the installation of CUDA, cuDNN and GPU versions of PyTorch. The following steps will guide you through the process: CUDA and cuDNN installation determine CUDA version compatibility: Use the nvidia-smi command to view the CUDA version supported by your NVIDIA graphics card. For example, your MX450 graphics card may support CUDA11.1 or higher. Download and install CUDAToolkit: Visit the official website of NVIDIACUDAToolkit and download and install the corresponding version according to the highest CUDA version supported by your graphics card. Install cuDNN library:

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

Complete Guide to Checking HDFS Configuration in CentOS Systems This article will guide you how to effectively check the configuration and running status of HDFS on CentOS systems. The following steps will help you fully understand the setup and operation of HDFS. Verify Hadoop environment variable: First, make sure the Hadoop environment variable is set correctly. In the terminal, execute the following command to verify that Hadoop is installed and configured correctly: hadoopversion Check HDFS configuration file: The core configuration file of HDFS is located in the /etc/hadoop/conf/ directory, where core-site.xml and hdfs-site.xml are crucial. use

PyTorch distributed training on CentOS system requires the following steps: PyTorch installation: The premise is that Python and pip are installed in CentOS system. Depending on your CUDA version, get the appropriate installation command from the PyTorch official website. For CPU-only training, you can use the following command: pipinstalltorchtorchvisiontorchaudio If you need GPU support, make sure that the corresponding version of CUDA and cuDNN are installed and use the corresponding PyTorch version for installation. Distributed environment configuration: Distributed training usually requires multiple machines or single-machine multiple GPUs. Place

The command to restart the SSH service is: systemctl restart sshd. Detailed steps: 1. Access the terminal and connect to the server; 2. Enter the command: systemctl restart sshd; 3. Verify the service status: systemctl status sshd.
