Introduction to the difference between pointer parameters and address parameters in c++ functions

  • 2020-04-01 21:25:28
  • OfStack

For example, a function chat (link &a); Chat (ling *a); The former introduces an address as a parameter is it possible to put a pointer variable p. Chat of p; What's the difference between that and the second function? Little brother can't understand ~~

Chat (int&a); Chat (int * a); These two functions are completely different things, and your understanding is mainly in int&a and int* a. Notice that int& and int* are two completely different types. Int & is the reference type, and int* is the pointer type to a variable of type int. Void chat (int&a) {a = 20; } call this function: int x=100; Chat (x); Void chat (int*a) {*a=20; void chat (int*a) {*a=20; } call: int x=10; Chat (& x); // the value of x will also be 20; This is where they have a connection, that is they can both change the value of the outside variable inside the function, but the two arguments are passed in different ways: void chat(int&a), this function is passed by reference, and void chat(int*a); This function is a value pass (although the value passed is an address value, it is still a value pass). How should this reference type be understood? Here's how I understand it: int a=100; Then what on earth is a, we say that a make we define a variable, the variable is a what things, don't know if you have thought of, we don't talk about compiling principle, but you can view a as is such a thing, a represents a piece of memory space, note: is a piece of memory space, that is to say that a piece of memory space may consist of one or more bytes, so in vc + + 6.0, int variables of type of 4 bytes, so a represents four bytes of contiguous memory space. So int & b = a; After this sentence is defined, b and a both represent the four bytes of memory space. Int times c is equal to &a; Now what is c? C also represents a block of memory space, in VC ++6.0, int* type variables account for 4 bytes of memory space, so c represents a continuous 4 bytes of memory space, the value placed in its internal is the address value of the first byte of the block of memory space represented by a. So when we pass parameters to chat(int&a), for example, chat(x), where a and x are also a block of memory space, and when chat(int*a) is called, chat(&x); And then the value in a is just going to be an &x, which is the first address of the memory that x represents. So you have to understand that they are not all passing addresses!! Address and reference are not the same thing in C++!!

Related articles: