Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C++ Expert Guidance (About Pointers and References)
C++ Expert Guidance (About Pointers and References)
The topic you asked is too big. There's a lot to talk about pointers and references.

My understanding:

Pointer: In fact, the concept of pointer is that in Tan Haoqiang's book, the pointer is the address and the pointer value is the address value. Pointer variables are used to store pointers.

Variables, so be sure not to confuse pointers with pointer variables. A pointer is just an address value, and a pointer variable is a variable that stores pointers (that is, addresses).

Definition of pointer:

Such as an integer pointer: int * p;; P is a pointer variable to data of type int. The address stored in it (that is, the pointer) is the address of an int variable. pointer variable

Typing, for example, the type of p is int *, which means that p is a pointer variable pointing to int type. How do I know the type of pointer variable? The easiest way is to delete variables.

The quantity defines the variable name in the statement, and the rest is its type. This method is applicable to all variable definitions, such as int a;; The type of is int. int b[ 10]; Class b

Type is int[], which means an array (I won't say array type here, because this problem is very subtle. In fact, there is no concept of array type in C and c++, including function type.

Is the same), int * c;; The type of c is int *. int * * d; The type of d is int * *, so it is very convenient to judge the type of variable in this way.

When it comes to pointer variables, we must say several aspects.

1. Pointer type.

This is very important. Type a pointer, that is, type an address. Specifically, an address will point to different types of data, which is different, for example.

int * p; P refers to int data. double * p 1; P 1 stands for double-precision data. But the space occupied by p and p 1 is 4 bytes (for a 32-bit system).

), if the C program is compiled with TC, it should be 2 bytes (dos operating system is 16 bits). Some people say that the address is just a value, similar to 0xfffdddcc.

Numerical values, why should they be divided into different types? What I want to say is that it has a lot to do with it. We know that pointers have operations, int * p = &;; a; So how much did p++ add? Don't think about it.

The address value in P plus 1 is a matter of course. In fact, so many bytes of sizeof(int) are added. In other words, an int element should occupy one byte.

For example, in an array, we can easily make the pointer variable point from the previous element to the next element in this way. P+5 has added so many p+sizeof(int)*5.

Bytes Another thing is that when a pointer is dereferenced, the type of the pointer determines how to interpret the binary data in the memory cell it points to. Such as int * p = &;; a;

Then the number obtained by (*p) is integer data, and if (*((char *)p)) is character data. P originally refers to int data (with 4 bytes)

The first address, if it is forcibly converted into a pointer variable pointing to char type, will only take the first byte of data when it is dereferenced and interpreted as ascii code.

Representative character. And if it is a pointer variable of a function, we can call the function directly through this pointer variable. Such as int(* function)(int); (int); now

Function points to a function with an int parameter and returns an int value. Then you can call this type of function through a pointer. Int a = function

( 100); Or int a =(* function) (100); In fact, it is better to quote or not to quote. But it used to need to be cited, and now the C standard stipulates that both methods can be used.

Generally speaking, the type of pointer is very important.

2. Pointers and arrays.

When we pass an array to a function, we usually pass the array name. We know that the array name is an address representing the first element in the array, and the array name cannot be empty.

Yes Actually, you haven't figured out what the array name is. Some people say that the array name is actually a pointer constant and cannot be changed. For example: int a[ 10]

={ 1,2,3,4,5,6,7,8,9, 10}; Then the possible type of a is int * const a;; Pay attention to the position of const (about pointer constants and const pointers, which will be said later), so this kind of

Is this statement correct? We can use the sizeof operator to calculate the storage space occupied by a certain data type. For example, the value of sizeof( 10) is 4. Attention, I have all the fakes here.

Compile on a 32-bit operating system. In fact, sizeof( 10) and sizeof(int) are the same, just to find out how much memory space this type of data takes up, which is not specific.

The space occupied by some data, because for the literal constant 10, it will not occupy memory space at all. In fact, there is no allocation, because the program is directly coded into the source program.

Memory problems. Then we calculate sizeof (a) like this; Did we get the 4 points we hoped for? The result may be unexpected, because its value is sizeof(int)* 10, that is

40 bytes, obviously this number of bytes is the number of bytes occupied by the whole array. Instead of the number of bytes occupied by pointer variables of type int *, we know that an address only takes up 4 bytes.

Then this shows that the array name is definitely not a simple int* type. But arrays do have the characteristics of int*. Such as int * p = a;; This can't be reported wrong. And what is passed between functions.

The same is true of time:

void print(int b[ 10]){}

Call function:

Print (one copy);

Or:

Print (& AMPA [0]);

That makes no difference.

Pay attention to the size of formal parameters in a function:

Invalid printing (int c[ 100])

{

sizeof(c); //At this time, the result of the expression is 4 instead of 100. Because here int c[ 100] is the same as int *c, and c is not an array name (array names cannot be left-valued.

).

}

3. Pointers and functions

A pointer can point to a function, a pointer can be used as a function parameter, and a function can return data of pointer type.

Pointer to function: a pointer to a function, which is actually the entry address of the function code. We can call the function like this. For example:

void print 1(int x)

{

Cout & lt& lt "Hello"<& ltx<& ltendl

}

Invalid print 2 (integer y)

{

Cout & lt& lt "Hello"<& lty<& ltendl

}

Then you can write this in the main function:

void(* p)(int)= print 1; //The function name represents the function entry address value. Like arrays, print 1 is more than just an address.

p( 10);

p = print2

p(20);

This is all possible, we can store pointers to functions through pointer arrays:

void(* aa[2])(int)= { print 1,print 2 };

for(int I = 0; I<2; i++)

{

Aa[i] (1); //Call the function inside through the function pointer loop.

}

For various statements pointing to function pointers, please refer to the relevant information yourself.

Quote:

A reference is equivalent to an alias, but you can just use it as an alias. The difference between a reference and a pointer: A reference must be initialized and cannot be changed after initialization. A pointer

But you can.