Feels like the powder’s slowed down lately. I had to let “Niigaki Noyi” out to attract attention.

The text before

This afternoon I watched the composition and design of the computer, and I couldn’t wait to see the design part — the design of the processor. God, I am still just a guy who is going to install a computer for people to do experimental field, even can’t use it well, you still ask me to design!! Although I have learned the principle of digital electric module and circuit, I have also learned the composition and application of single chip microcomputer, but it does not mean that I am interested in these logic components!! So I decided to abandon chapter 4 and jump to chapter 5 — Storage hierarchy and Concurrent design — by dinnertime, which gave me the courage to live again. Then evening continue to see the god book – “C++ Primer fifth edition”, here is really just to see me infatuated, with a lot of principles in computer composition to see the knowledge, every day the biggest harvest will be this time! The following is today’s reading notes, feel a lot of major companies recruit IT talent pen questions of the multiple-choice category!!

The body of the

1. Continue with the quotation from the previous section

  • A reference is just an alias and has no actual address or object. It just binds an object, like a domain name to an IP. But the Pointers are different. The key is the following:
  • The pointer itself is an object, which can be copied and copied, and can operate directly on the pointer object itself, unlike references that have no object entity and have to rely on other objects to survive.
  • A pointer does not need to be assigned an initial value when it is defined, because it is an object that claims storage space and does not need to be identified at first.
  • As with other built-in types, a pointer defined in block scope is an indeterminate value if it is not initialized.

2, when the definition of pointer, there is a saying in the book, as follows:

int vdal; int *pd=&vdal; // Correct: the initial value is the address of the variable int *pd1=vdal; // Correct: the initial value is a pointer to an intCopy the code

For third line that I am always suspicious of the case, because when running on my computer is can’t run, directly to an error, so we define a pointer, or direct method, according to the definition of the second line that may be the third line that play is too high, anyway I now ~ ~ of the compiler is running out

An error!

In addition, when declaring a pointer, the type of the pointer should be the same as the type of the object to which it points. Otherwise, it is easy to make errors, such as the following code:

double dval; int *pd=&dval; // Error: a pointer to an int cannot point to a double.Copy the code

But sometimes there are special cases, after the face I slowly introduce.

3. The value of pointer

Four states:

  • Point to an object
  • Points to the next location in space occupied by the immediate object
  • A null pointer means that the pointer does not point to any object
  • Invalid pointer, that is, any value other than the above

Attempts to copy or otherwise access the value of an invalid pointer raise an error. The consequences of the visit are unpredictable.

4. Use Pointers to access objects

If the pointer is to a variable, it can be accessed directly with *p. If it is to a structure, it can be accessed in one of two ways:

Struct * Item; *Item.name="ZZB"; item->name="ZZB";Copy the code

The second and third lines above have the same meaning. The first line defines a structure that has a variable name inside it, so if you want to access name. It’s exactly the same. The dereference (*) operation applies only to valid Pointers to objects. Here are some interesting and confusing definitions or declarations. Let’s explore:

int i=42; // This is the definition of int &r= I; // This is a reference to int *p; // To define a pointer, you see, it doesn't have to be initialized, right? // assign the address of I *p= I; // This is the same meaning as above. That is, assigning I to the dereferenced pointer to P is equivalent to assigning I's address to P; int &r2=*p; // The p pointer is dereferenced by R2Copy the code

Here’s a bit of common sense in computing: Read a piece of code, from right to left, in order of priority.

Null pointer

A null pointer does not point to any object. Here are some ways to generate a null pointer:

int *p1=nullptr;
int *p2=0;
#incluede 
      
        int *p3=NULL;
      Copy the code

The above three methods of null pointer definition are completely equivalent, the most direct is the first one. If you really don’t know where the pointer points to during programming, you can choose one of the above three options.

Void * pointer

Void * is a special pointer that can hold any type of address, which means you can copy any type of address, like a moving van. But there is no way to find the contents of any object it points to, because you can’t be sure what type of object it points to, so there is no way to prepare a register of the corresponding type to hold the value of the retrieved object.

int p=23; Double q = 1.2; void *a=&p; a=&q;Copy the code

All of the code above will not an error, because the void pointer is so slip, can accept any form of objects pass a pointer to it, but also because such a 6, so whether the object is simple, you void can’t call, from the Angle of the void pointer, just point to a memory address space, as for the inside is what, I don’t care, I just kept this address in mind.


int a=12;
void *p=&a;
cout<<(double)*p<<endl;Copy the code

Error reported. Love dearly

7, those years defined the pointer to step on the pit:

  • First,, are these two meanings different?
    int* p;
    int *p;Copy the code

    Actually, there’s nothing to use. It just makes you look alert. There really isn’t any difference, but the first way is misleading.

  • The second one, I’ve defined two Pointers, right?
    int *p1,*p2;
    int* q1,q2;Copy the code

    However, the reality is often cruel, the first line is defined two Pointers, but unfortunately the second line is a pointer, an integer number, as for who is who, HOW do I know, guess? !!!!!

A reference to a pointer

Please check the following code:

int i = 42 ;  
int *p ;   
int *&r=p;  
r=&i;   
*r=0;Copy the code
  • Start with line 3: This is the beginning, remember what we said earlier?The code reads from the right,A reference to a pointer is defined. Here r is just a reference to the pointer object p, which is another name for P, intIt’s one. Cout <<r<<endl; ` ` ` get 42;
  • The fourth line defines the value of the reference, i.ep=&i;
  • Need not say more about the fifth line? That’s the same thing as assigning to I*p=0 or i=0;

9, const qualifier

  • Basically, const means that whatever I’m defining is untouchable, and no one can touch me except to modify me, but if you try to modify me in any other way, sorry, you’re dead. . But if the inside is an address, you found another object through my address, so change with you, you delete can, but do not move this address, or I am anxious with you!! That’s right, const protects the defined object from modification. Anything that threatens a const object is treated as an error feedback
  • One more thing, const must be initialized, otherwise I’m protecting an object, and it’s not even initialized, so const must be initialized, or I’ll get an error. See code examples:
    int  i = 42;
    const int j=i;
    int k=j;   //Right!!
    j=100  //Error!!Copy the code

    Obviously, the third line means I can fetch a const constant, but the fourth line means I’m dying to change a constant.

10. By default, const is only valid within a file

If you want to run to another file:

  • One way is to duplicate the definition, but this actually amounts to defining many song independent constants in different files, which is prone to errors. So that’s exactly what we have to avoid, we have to do it another way.

  • For const variables, either declared or defined, we add an extern that we learned last time. This way you only need to define it once and declare it in another file, but remember that only one of them can be initialized, and all the others are just declarations, otherwise it will be great. So, how does extern get initialized? Isn’t that the same as the definition? That’s the beauty of extern being initialable!

A reference to const

  • Initialization and reference to const

    int i=42;
    const int &r1=i;Copy the code

    It is allowed to bind a constant reference to a nonconstant, but since the reference itself is not an object, we cannot restrict the fact that I is non-constant by r1 reference!

    Const int & r2 = 42;Copy the code

    Yes, it is a constant reference! Where 42 is a constant, a reference to a constant must be defined as a constant reference.

    const int &r3=r1*2 ;Copy the code

    R1 is itself a constant, so it makes sense to define a constant reference bound to a constant!

    int &r4=r1*2;Copy the code

    Error: No!! R4 is not a constant reference, which means that the outside world can change the object it’s bound to by changing R4, but you want to bind constants? Have a dream! Error reported!!

  • conclusion

      1. To reference a constant, it must itself be a constant reference or a constant pointer;
      1. A constant reference object does not have to be constant, but can be non-constant, as shown in the second line above

###12, pointer and const

Pointers are objects, references are not, so it is possible to define the pointer itself as a constant rather than a constant reference. So!!!!! A constant pointer must be initialized, and an address must be assigned to the constant pointer object. What does not change is the value of the pointer itself, not the value to which the pointer points:

int numb=0; int *const err=&numb; Const double PI = 3.14; const double *const pip=&piCopy the code

  • Look at the second line, starting from the definition of err, going from right to left, first we get a const which means it’s a constant, then we get an * which means it’s a constant pointer, then we get an int which means it’s a constant pointer to an int, so what does it initialize? Yes, numb’s address, perfect match!! Here we define a constant pointer to a nonconstant. We can modify the object to which the pointer points, but we cannot modify the pointer. In contrast, if the pointer points to a constant and the pointer itself is not a constant pointer, we can modify the object to which the pointer points.

  • Look at line 4, starting from PIP from right to left, const — constant, * — pointer, double — pointer to a constant of type double, const means to a constant. Not only is PIP itself a constant pointer to a double, but the object it points to is also a constant of a double!

So our following operations are legal:

*err=100; cout<<numb<<endl; //output: 100 instead of 0; cout<<*pip<<endl; / / the output: 3.14 cout < < PI < < endl; / / the output: 3.14Copy the code

So, in summary, const is an arrogant guy! Oneself is completely cannot change, once have this trend, li Mara whole program launch, report error!! If you refer to someone else, it doesn’t matter if that person is a constant, but if they refer to you, they must be a constant! If you’re using a pointer to a constant, you can’t change it with a pointer, you can only reference it, and you can’t change it with a pointer! Even though this thing is not beautiful! But why do I love this stuff so much?

After the body

Want to sleep, tonight a little wave, tomorrow continue to read, but evening want to see “shy iron fist” do not know is not to say with others in that way laugh point dye-in-stone, then change to sleep!