C + + references, pointers

Source: Internet
Author: User

First, reference

1. The role of the reference: to give the variable an alias, is C + + on the expansion. The original name and alias have the same address, which is basically the same thing, but the names are different. the reference mechanism of C + + is mainly to be used as function parameter, to enhance the ability of function to pass data, such as swap function, the reference is to modify the argument directly.

2. Declaration method:

int A;
int &b=a;

& immediately after the data type, it is the reference declarator, and the other place is the address character.

3. Differences with pointers:

When declaring a reference, it must be initialized at the same time, and then no more modification, no pointers;

The reference cannot be changed after initialization, the pointer can;

References cannot point to null values, pointers can be;

Cannot reference an array or array element;

A reference cannot be established;

Pointers cannot point to references;

Second, function parameter pointer

1. Calling a function does not change the value of the pointer, but it may change the value pointed to by the pointer.

three, array pointers

1. Assuming that the pointer P has pointed to an element in the array, P+i points to the next I element of the element, in fact, P+i=p+i x sizeof (DataType).

2.c++ is to handle the array in the function parameter as a pointer, that is, the function call does not exist in the array, only the pointer.

Four, string pointers

1. A way to define a string:

Char str[]= "ASDASD";

The definition can not write the number of elements, but must be initialized, int what can also be defined as such, and then automatically recognize the number, char will end up with '.

The string defined by 2.char* can be accessed using [], but cannot be modified.

Five, function pointers

1. The pointer to the function must use "()", for example (*p), because of the priority problem, if there is no parenthesis is the return value as a pointer function. such as int *a (int x,int y), is the function that returns the value as a pointer.

2. A pointer to a function can be used as a function parameter, in order to pass the function name to a function parameter, it is uncommon to call a function with a different argument and then invoke another function.

vi. array of pointers

1.int *p[4]= (int *) P[4] is an array of pointers, and the elements in the array are pointer types.

int (*p) [4] is a pointer to an array, and the element in the array is int.

seven, void * pointers

1. (void *) pointer type is an indeterminate type instead of any type and must be converted to a certain type to be used, so it is just a transition.

2. You can assign a non-void * to void *, but not vice versa.

e.g.

int a=89;
int *p1=&a;
void *p2;
p2= (void *) P1;
* (int *) P2 is A;

C + + references, pointers

Related Article

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.