C language Pointers and C + + references

Source: Internet
Author: User

Learn from C and C + +, and sort out questions about pointers and references

Pointer:

What is a pointer?

The pointer itself is a variable that stores the address of the data in memory rather than the value of the data itself, the type of pointer, the type that the pointer is pointing to, the value of the pointer, or the memory area that the pointer is pointing to, and the memory area that the pointer itself occupies is the problem we want to consider

1, int a=0,p; P =&a;

2, int a=0; *p=&a;

The first defines the method and assigns the address of A to P. The second is to assign the address of a to pointer p while the pointer p is defined. I understand that an int * is a flag that defines a pointer

When you remove the pointer p, you can see the type of the pointer:

(1) The type of char*p;//pointer is char*

(2) int* (*ptr) [4];//pointer type is int* (*) [4]

Remove *p to see what type of content the pointer is pointing to the area

(1) The type of the char*p;//pointer is char

(2) int* (*ptr) [4];//pointer type is int* () [4]

We can find the address of the variable A that the pointer points to by *P, and then manipulate the value in the address.

printf ("%p", p)  result is P pointing to the address of a, printf ("%d", *p) result is the value of variable a printf ("%d", &p) The result is the address of the pointer p

Cite an example that you see:
  1. int *p; //First start at p, first with *, so that p is a pointer and then combined with int, indicating that the pointer points to the type int. So p is a pointer that returns an integer data
  2. int p[3]; //First start at p, first with [], stating that P is an array and then combined with int to indicate that the elements in the array are integral, so p is an array of integer data
  3. int *p[3]; //First start at p, first with [], because its priority is higher than *, so p is an array, and then combined with the *, that the element in the array is a pointer type, and then combined with an int, indicating that the pointer to the type of content pointed to is integer, so p is an array of pointers that return an integral type of data
  4. Int (*p) [3]; ///First start with the *, with the first, the P is a pointer and then the [] combination (and "()" This step can be ignored, just to change the priority), indicating that the pointer to the content is an array, and then combined with int, the elements in the array is an integer type. So P is a pointer to an array that consists of integral types of data
  5. int **p; ///first starting with the p, the first combination with the *, said that P is a pointer, and then combined with the *, indicating that the pointer points to the element is a pointer, and then combined with an int, indicating that the pointer to the element is an integer data. Level Two pointers are typically used in data structure programming
  6. Int (*p) (int); //Starting at p, first with the pointer, stating that p is a pointer, and then combined with (), indicating that the pointer is pointing to a function, and then in () with the int, that the function has an int parameter, and then with the outermost int, the return type of the function is integral type, so P is a pointer to a function that has an integer parameter and returns a type of integer
  7. int * (*p (int))) [3]; //Starting with P, first with (), that P is a function, and then enter (), and int, that the function has an integer variable parameter, and then with the outside of the *, the function returns a pointer, and then to the outermost layer, first and [] union, Indicates that the returned pointer points to an array and then to the *, indicating that the element in the array is a pointer, and then combined with an int, indicating that the pointer is to the integer data. So P is a function that has a parameter that is an integer data and returns a pointer variable that points to an array of integer pointer variables .
The & is the address operator, and * is the indirect operator. 
The result of the &a operation is a pointer, the type of the pointer is the type of a and the type of the pointer is the type of a, and the address to which the pointer points is the address of a.
The result of *p is what p points to, something that has these characteristics: its type is the type P points to, and the address it occupies is the address that P points to.
Here is an example: Int **ptr=&pa; //&pa In this sentence *ptr is the pointer pa, but *ptr occupy the memory, so be put on the right

Array names of arrays can be viewed as a pointer
arr[0] = = *arr;
arr[3] = = * (arr+3)
or char *str[3]={"hehe!","Haha.","Helloworld"};

STR is an array of three units, each of which is a pointer to a string.
If STR is a pointer, it points to the No. 0 unit of the array, which is of type char * *, which is the type char *.
str+1 is also a pointer to the 1th element of the array, whose type is char**, and the type it points to is char*.
*STR is also a pointer to the type char *, which is the type char, and the address it points to is the string "hehe!" The address of the first character, which is the address of ' H '.

struct and pointer:
struct NUM
{
int x;
int y;
}
struct num num = {20,30};
struct *PTR = # when accessing a member Ptr->x,ptr->y
int *pstr = (int*) # Access Member (*PSTR) (* (pstr+1))

references:

A variable is preferable to multiple aliases.

General reference Examples

Answer:

This shows that the A,b,c address is the same

Let's look at one more example.

This problem output A = = b = = 20, at this time the value of B cannot be modified, but can modify a to modify B, also, only the constant reference can refer to a const modified variable.

void S(int& A, int& b)//Use of references, no temporary copy,& instructions here is just another name for the original argument. So Modify the value of the variable directly on the basis of the original parameter.

void S(int* a_p, int* b_p)//is the address that is passed in, because the address is unique, so the pointer is accessed through the address to modify its contents.

Summarize the following points:

(1) A reference is a "nickname", another name for an identifier. & Here is not an address operation, but an identification function.

(2) The type identifier refers to the type of the target variable, which can reference not only the identifier, but also the immediate number (rvalue, literal, constant), but must be the const attribute.

(3) When declaring a reference, the reference must be initialized at the same time, or a compilation error occurs.

(4) After the declaration is complete, the equivalent of the target variable name has two names, that is, the target name and reference name, and can no longer use the reference name as an alias for other variable names, that is, the reference can only be one-time, can not be changed (once the reference succeeds, it is a variable of this identity).

(5) The address of the reference, that is, the target variable to find the address. That is, the reference name is an alias of the target variable name. The reference is defined as saying that the reference does not occupy any memory space, but the compiler will generally

This is actually a const pointer, which is a pointer to a position that is immutable, so the reference actually uses the same memory as the generic pointer.

(6) An array of references cannot be established. Because an array is a collection of several elements, you cannot create a collection that consists of references, but you can establish a reference to the array.

(7) Reference common use: As a function of the parameters, functions of the return value, but must not return a reference to a local variable.

(8) A null reference cannot be defined, but a "wild reference" or "dangling reference" is present (data that references the heap memory and should not be used once the heap memory is freed).

(9) A reference can be used as a function parameter, and the object it refers to is the argument of the function, so the reference can achieve the effect of the pointer.
A, shared variables between functions
b, improve the efficiency of the transfer of parameters (higher than the pointer)
c, can get parameters

But a reference cannot completely replace a pointer.

References and pointers differ and relate to:

Different points:

1. The pointer is an entity, and the reference is only an individual name;

2. The reference does not need to dereference (*), the pointer needs to be dereferenced;

3. References can only be initialized once at the time of definition, and then immutable; pointers are variable;

4. The reference does not have a const, the pointer is Const;const modified pointer is immutable;

5. The reference cannot be null, the pointer can be empty;

6. "sizeof Reference" gets the size of the variable (object) pointed to, and the "sizeof pointer" gets the size of the pointer itself (the address of the variable or object to which it is pointing);

7. The pointer and the reference self-increment (+ +) operation has different meanings;

8. From memory allocation: The program allocates memory regions for pointer variables, whereas references do not need to allocate memory areas.

9. The pointer indirectly operates on the variable it points to by pointing to an object through a pointer variable. The use of pointers in the program, the readability of the program, and the reference itself is the alias of the target variable, the operation of the reference is the operation of the target variable.

The same point: both are the concept of the address, the pointer points to the memory, the content is the address of the referred memory, the reference is the alias of a block of memory

C language Pointers and C + + references

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.