C ++ father of learning Note 3 -- pointer

Source: Internet
Author: User

I have not studied C ++ for a long time because I need to turn my work into C #. I have studied C ++ for a while and I have written two articles in my blog, now I have a special C ++ blog and I will write it here later. The addresses of the first two articles are as follows:
C ++ father of learning notes 1 C ++ father of learning notes

Today, let's briefly talk about pointers. We can say that pointers are a difficult point and a double-edged sword. Maybe they can be used freely. cainiao like me will hurt myself. Well, let's talk nonsense.
Pointer: For type T, T * is the "to T Pointer", that is, a variable of type T * can save the address of an object of type T. For example, char c = 'a ';
Char * p = & c; // p stores the address of c
Shown in the figure below:

These are easy to understand. Unfortunately, getting to the array pointer and getting to the function pointer requires a more complex Notation: int * pi; // getting to the int pointer
Char ** ppc; // pointer to the character pointer
Int * ap [10]; // an array of 10 to int pointers
Int (* fp) (char *); // pointer to the function. This function uses char * as the parameter and returns int
Int * f (char *); // a function with the char * parameter. a pointer to the ini is returned.
The basic operation on the pointer is indirect reference, that is, to reference the object referred to by the pointer. This operation is also called indirectionn ). The indirect operator is (prefix) One dollar *. For example, char c = 'a ';
Char * p = & c; // p stores the address of c
Char c2 = * p; // c2 = 'A' the variable referred to by p is c, and the value stored in c is 'A '. Therefore, the value assigned by * p to c2 is 'A '.
Note: here we will explain that the indirect operator is (prefix) One dollar *. This involves the Declaration structure.
A declaration consists of four parts: an optional "descriptor", a basic type, a declaration, and an optional initial formula. Except for functions and namespaces, other declarations should end with semicolons. For example: char * kings [] = {"Antigonus", "Seleucus", "Ptolemy"}; the basic type here is char, and the declaration is * kings [], the initialization is = {...}.
Descriptors are a starting keyword, such as virtual and extern. They indicate some non-type attributes of declared objects.
A declarative operator consists of a name and several optional declarative operators. The most common declaration Operator is
* Pointer prefix
* Const constant pointer prefix
& Reference prefix
[] Array suffix
() Function suffix

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.