In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is a pointer in C++, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
What's a pointer?
A pointer is the address of a variable in memory.
Data is stored in memory, each variable has a memory address, assuming that it is an int type variable a, accounting for 4 bytes of memory area, then if it is stored in memory in a small way, we create a pointer p, assign the address of a to p, that is, assign the first address of a to pointer p, and the value of p is the first address of variable a in memory.
int a =10;int*p;p=&a; //assign the first address of a to P
That is, the pointer is the first address of the memory region.
memory address
Memory is a large, linear array of bytes, each byte is composed of fixed 8 binary bits, the computer for each memory unit are numbered, this number is called memory address, address determines the location of memory units in memory
memory occupied by pointer
In a 32-bit system, the maximum number of bits a processor can process at a time is 2 to the 32nd power, which is 4 bytes of memory data at a time, and the length of the pointer is 4 bytes.
In a 64-bit system, the maximum number of bits a processor can process at a time is 2 to the 64th power, which is 8 bytes of memory data at a time, and the length of the pointer is 8 bytes.
32 bit system pointer 4 bytes 64 bit system 8 bytes
const variable
const is an abbreviation of constant. const means constant, not easily changeable
That is to say, variables modified with const have values that are not allowed to change
const int a = 7; int b = a; //correct a = 8; //error, cannot change pointer constant, constant pointer and pointer to constant
After understanding the basics above, let's look at what pointer constants, constant pointers, and pointers to constants are.
First of all, how to quickly remember their differences, just need to read according to the order of naming, you can remember well
For example:
int * const p =&a;
First it's a pointer int * , then it's a const constant, so p is a pointer constant.
const int *p=&a;
First it's a const constant, then it's a pointer int *, then p is a constant pointer.
Is it easy to remember?
pointer constant int * const p =&a;
Features: Pointer pointing can not be modified, pointer to the memory value can be modified
Because the function of const is to limit that it cannot be modified, we see that pointer constants have int * pointers first, and then const modifies p, so the value of p (pointing to memory address) cannot be modified.
That is, const limits the value of pointer p, but there is no limit on the data in the memory address that p points to, so the memory data can be modified.
Examples:
int a=10;int * const p =&a; //Define pointer constant, pointing to address of int a *p = 20; //Correct, data in memory address pointed to can be modified p=&b; //error, the memory address pointed to cannot be modified
The arrow is the memory value pointed to by the pointer, which cannot be modified, but the data in the memory address pointed to can be modified.
int *p=&a;
Features: Pointing to the pointer can be modified, but the pointer to the value can not be modified.
Constant pointers, first of all constant. That is, the value pointed to should be a constant, but the memory address pointed to is not limited.
int a=10;int b=10;const int *p=&a; //Define a constant pointer to int a's address *p = 20; //Error, the data in the memory address pointed to cannot be modified p=&b; //correct, the memory address pointed to can be modified
The arrow is the memory value pointed to by the pointer, which can be modified, but the data in the memory address pointed to should be consistent with the previous one.
const int const *p=&a;
Features: Pointer pointing can not be modified, pointer pointing to the value can not be modified.
Two const restrictions, once pointed, can not be modified
int a=10;int b=10;const int const *p=&a; //Define pointer constant, pointing to address of int a *p = 20; //Error, data in memory address pointed to cannot be modified p=&b; //error, the memory address pointed to cannot be modified
null pointer
A null pointer is a pointer to a null address.
Note: The memory pointed to by the null pointer is inaccessible
//-----null pointer------/int *p4 = NULL;//cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.