当前位置:网站首页>9. Pointer of C language (1) what is pointer and how to define pointer variables
9. Pointer of C language (1) what is pointer and how to define pointer variables
2022-07-28 20:05:00 【A programmer who loves playing badminton】
This article is the... Of the pointer series 1 piece , If it feels good , You can follow and collect , It will be updated continuously in the future
Pointer is C The soul of language
C Language can be used by pointer
Represents some complex data structures
Fast data transfer
Make the function return more than one value
Direct access to hardware
It can easily handle strings
Before learning the pointer , We need to understand the concept and relationship between address and pointer
Address : Refers to the number of memory unit , It's from 0 The starting nonnegative integer , In the computer , Byte is the basic unit of data storage , And it is also the smallest unit that the hardware can access , A byte is a number .
The pointer : The pointer is the address , The address is the pointer , It's a value , It's the number
Pointer to the variable : Is a variable for storing addresses , What we need to learn is pointer variables , Usually, when we describe, we will abbreviate pointer variables as pointers , Actually, they have different meanings
Let's learn how to define pointer variables through a simple example
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, char const *argv[])
{
int *p; //p It's the name of the variable ,int* Express p Variables are stored in int The address of a type variable
//int* p; Does not mean that a name is defined *p The variable of
//int* p; It should be understood that :p Is a variable name. ,p The data type of the variable is int* type
// So-called int* type , It's actually storing int Type of variable address
int i = 3;
p = &i;
/*
p Save the i The address of , therefore p Yes i
p No i,i Neither p, modify p The value of does not affect i Value , modify i The value of does not affect p
If a pointer variable points to a variable , be * Pointer variables are exactly the same as ordinary variables
Example :
If p It's a pointer variable , also p Stored ordinary variables i The address of
be p It points to the normal variable i
*p Is exactly the same as i
Or say : In all appearances *p All places can be replaced with i
*p That is to say p The content of the address is the variable
The pointer is the address , The address is the pointer , It's a value , It's the number
The address is the number of the memory unit
A pointer variable is a variable that holds an address
But be careful : Usually, when we describe, we will abbreviate pointer variables as pointers , Actually, they have different meanings
*/
// p=i; error, Because of the different types ,p Only store int The address of a type variable , Cannot be stored int Type value
int j;
j = *p; //*p Namely i
printf("i=%d,j=%d\n", i, j);
return 0;
}The output is i=3,j=3
summary : What we need to know is
1.int *p Its meaning is to define a pointer variable p,p It stores the address of the integer variable
p No i,i Neither p, modify p The value of does not affect i Value , modify i The value of does not affect p
2. Assignment statement p=&i; The meaning of &i Is to take i The address of , So this sentence expresses will i The address of is placed in the variable p in

We wrote p=&i After this sentence ,P It points to i Variable , At this time ,P The storage address is 1000H

3.*p Means to store in p The content of (1000H) Variable for address , In fact, it is 3
here *P Is equivalent to i
边栏推荐
猜你喜欢

Source insight project import and use tutorial

【NPP安装插件】

云计算笔记part.2——应用管理

Basic knowledge of communication network 01

11. Learn MySQL union operator

Concurrent programming, do you really understand?

English translation Arabic - batch English translation Arabic tools free of charge

Basic usage of docker

How navicate modifies the database name

JVM(二十四) -- 性能监控与调优(五) -- 分析GC日志
随机推荐
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
Const pointer of C language and parameter passing of main function
[C language] shutdown game [loop and switch statement]
JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
Saltstack advanced
HSETNX KEY_NAME FIELD VALUE 用法
JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
Using Lex (Flex) to generate lexical analyzer of PL language
[network] cross area network communication learning classification and calculation of IPv4 address
2、 Relationship between software operation and memory
[C language] simulation implementation of strlen (recursive and non recursive)
[C language] advanced pointer exercise 1
Circular linked list OJ question
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Intermediate soft test (system integration project management engineer) high frequency test site
Advanced notes (Part 2)
2. Floating point number, the difference between float and double in C language and how to choose them
Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022
Know small and medium LAN WLAN
MySQL command statement (personal summary)