当前位置:网站首页>2 variables and basic types
2 variables and basic types
2022-06-12 06:39:00 【A building climbing pig】
2 Variables and basic types
2.1 Words and bytes
1、 The bit (bit): Computers store data in bit sequences , Each bit is not 0 namely 1.
2、 byte (byte): Most computers operate in 2 The integer power of bits is used as a block to process memory , The smallest addressable block of memory is called a byte
3、 word (word): The basic unit of storage is called a word , It usually consists of several bytes .
Generally, the bytes of a computer consist of 8 Bit formation , And a word is usually 4 Bytes or 8 Bytes .
2.2 quote ( lvalue reference )
1、 A reference is an alias for an object ;
2、 References must be initialized , And the initial value of the reference must be a object ;
What is an object ?
Generally speaking, an object is a piece of memory space that can store data and has some type of memory . Some people distinguish between named objects and unnamed objects , They call named objects variables . Others distinguish between objects and values , The object refers to the data that can be modified by the program , And value (value) Refers to read-only data .
int &a = 3 // error
3、 The reference itself is not an object , So you can't define references to references ;
4、 Multiple references are allowed to be defined in one statement , Each reference identifier must be represented by the symbol & start
Key points : An operation on a referenced variable is an operation on the object it references !! So the formal parameters of some functions are often reference types .
2.3 The pointer
1、 Understand pointers , Need to understand * Symbols and & Symbol ,* Can be used to define pointers , It can also be used to dereference ,& It means to get the address .
2、 A null pointer doesn't point to anything .int *p=nullptr Define null pointer .
3、void* The pointer can store the address of any object , You can compare the size , Because there is no type , Only operate on memory space , Unable to access or dereference the stored object , Cannot force offset , Can be replaced by forced type static_cast Replace with other types of pointers .void * Pointer variables are mainly used to pass different types of pointer variables to functions , Cast it to a specific pointer type inside the function .
4、 The pointer to the pointer , Pointer to the variable p It is also a variable occupying an independent memory block , Its own address can also be saved in a In pointer variables , This pointer variable is often referred to as a pointer to a pointer , representative ** Symbol .
5、 Reference to pointer , The reference itself is not an object , So you can't define a pointer to a reference . But the pointer is the object , So there are references to pointers ,*& Symbol , Look from right to left .
int i=42;
int *p;
int *&r = p; // Look from right to left , Pointer type reference
r = &i; // establish , be relative to p = &i
Comparison of references and pointers :
Reference variables are just aliases for other variables , No independent memory , The same reference variable cannot be modified to reference different variables . Pointer variables store the addresses of other variables , There is independent memory , Can point to different objects at different times .
2.4const Modifier
1、const I want to define some A variable whose value cannot be changed .
2、const Object must be initialized , And cannot be changed .
3、const Variables cannot be accessed by other files by default , No access , Must specify const Add... Before definition extern. To use in multiple files const Variable sharing , Definitions and declarations are added const Key words can be used .
4、const And a pointer , Include pointer to const and Constant of pointer type .
5、 pointer to const (pointer to const), Cannot be used to change the value of the object it refers to , There are two ways to define it , Take the integer constant as an example , The definition includes int const* p = &i and const int* p = &i. Indicates that the object pointed to by the pointer is a constant , The pointed object cannot be modified .
5、 Constant of pointer type is also called constant pointer (const pointer), The pointer itself is a constant , There are several ways to define it int *const p = &i;, That is, the pointer is of constant type , That is, the pointer is fixed to the object , The address stored in the pointer remains unchanged , But the object value corresponding to the address can be modified .
6、const And quotes , A reference to a constant (reference to const), Point to const References to objects , Such as const int i =1; const int &r = i;, Variables can be read but not modified r.
Provisional quantity (temporary) object : When the compiler needs a space to temporarily store the evaluation results of the expression , An unnamed object created temporarily . A reference to a temporary quantity is illegal .
2.5 Type the alias typedef
1、 Type the alias (type alias) It's a name , It is a synonym of some kind . There are many benefits to using type aliases , It makes complex type names simple 、 Easy to understand and use , It also helps programmers to be clear about the real purpose of using this type .
2、 Traditional definition , Use keywords typedef, Such as typedef Sales_item SI
2、C++11 New feature definitions ,using SI = Sales_item
3、 Understand pointer types typedef The difference between : This object that matches the type , It cannot be understood by substituting the original formula, e.g :
typedef char *pstring; // pstring yes char* Another name for
const pstring cstr = 0; // Constant of pointer type of , Point to char type
const pstring *ps; // here ps It's a point char Pointer to type constant
// It's like const char *cstr = 0; Incorrect , To point to const char The pointer to
// To assist in understanding ( It can be replaced by parentheses )
// const pstring cstr = 0; After replacement const (char *) cstr = 0;
// const char *cstr = 0; That is to say (const char *) cstr = 0;
Again, pay attention to :
1、 How pointers to constants are defined const int* p = &i perhaps int const* p = &i,
2、 The constant of pointer type is defined as int const* p = &i,
3、 If you use typedef Words ,cons A constant that represents a pointer type , namely const pstring p Is a constant of pointer type , and const pstring *p Is a pointer to a constant .
边栏推荐
- Video summary with long short term memory
- ConVIRT论文详解(医疗图片)
- GET 和 POST 的区别及留言板代码实现
- Deep and detailed analysis of PHP one sentence Trojan horse
- LeetCode-1741. Find total time spent per employee
- Unreal Engine learning notes
- Codeforces Round #793 (Div. 2) A B C
- Process when solving vagrant up_ builder. rb:43:in `join‘: incompatible character encodings: GBK and UTF-8
- Multithreading (2) -- pipeline (4) -- Park and unpark
- Use ms17-010 Eternal Blue vulnerability to infiltrate win7 and establish a permanent back door
猜你喜欢

platform driver
![Leetcode: Sword finger offer 66 Build product array [application of pre and post infix]](/img/de/cd98d4d86017a13ec4172ba3054e99.png)
Leetcode: Sword finger offer 66 Build product array [application of pre and post infix]

PHP 读写 COOKIE

Use ms17-010 Eternal Blue vulnerability to infiltrate win7 and establish a permanent back door

SQL injection - blind injection

Nodemon cannot load the file c:\users\administrator\appdata\roaming\npm\nodemon PS1, because script execution is prohibited in this system

Touch screen setting for win7 system dual screen extended display

Bid farewell to the charged xshell, and the free function of tabby is more powerful
![[reinstall system] 01 system startup USB flash disk production](/img/0d/9b3d4b8e286a75f8b58e35d02f261b.jpg)
[reinstall system] 01 system startup USB flash disk production

Automatic modeling of Interchange
随机推荐
descheduler 二次调度让 Kubernetes 负载更均衡
Highlight detection with pairwise deep ranking for first person video summary (thesis translation)
leetcode 300. Longest increasing subsequence
六月集训 第一日——数组
Excel VBA opens a file that begins with the specified character
LeetCode-1741. Find total time spent per employee
leetcode:剑指 Offer 66. 构建乘积数组【前后缀积的应用】
Redis basic notes
June 9th training day - bit operation
LeetCode-1873. Calculate special bonus
8. 表单标签
使用 ms17-010 永恒之蓝漏洞对 win7 进行渗透及建立永久后门
Bulk Rename Utility
LeetCode-1303. Team size
About session Getattribute, getattribute error
Tomato learning notes -seq2seq
The seventh day of June training - hash table
Redis distributed lock
PHP 读写 COOKIE
Codeforces Round #793 (Div. 2) A B C