Array in C ++, pointer Parsing

Source: Internet
Author: User

Ah, because I didn't attend lectures during the class, the pointer is really understandable here. Although it has little impact on code writing, it is always accurate, so today, let's take the opportunity to figure out this stuff, and check it every time you save it. We do not ask for long articles, but for clear content ~

If anyone sees the problem, please help us to point it out, so as not to mislead others ~ Here, I would like to say thank you ~


In fact, it is better to know about arrays, that is, to allocate a certain type of group, thisThe Group length must be given or initialized.In the first place, I thought it was inconvenient because we do not know the length of the array in many cases. For example, this array needs to be defined by users, but there is no way, the computer will allocate a memory space for this array. If you don't tell the computer, the computer will be confused during compilation, and it will get wrong. So let's move to each other and give him a length, or an initial value is required.

 

Array declaration: In simple terms, arrays are stored from low dimension and 0. For example, the storage order of int a [2] [2] in memory is: a [0] [0]-a [0] [1]-a [1] [0]-a [1] [1]. yes, because the two numbers look like binary numbers. During the declaration, either the numbers in the brackets are given, or Initialization is given, for example, int a [2] [2]; or int a [] [2] =, }; of course, this is equivalent to int a [] [2] ={{}, {}. Note that low-dimensional numbers cannot be saved, 2 In '[2]' cannot be saved.

 

NextPointer.

First, you must know thatMemory Access ModeIn general, we areVariable nameTo access the memory, for example, int a = 1; we use a to access the memory that stores 1, but there is another way:Address used. For example, the school is our memory. Every student has his/her own name and corresponding student ID. The name is the variable name here, and the student ID is the address. In batch processing, to transmit a large amount of data, it is easier to access the data by student ID than by name. Therefore, you must master both methods.

Pointer: A pointer is actually a data type. A variable with a pointer type becomes a pointer variable, which is used to store the memory unit address. The declaration method is as follows:

// Data type * identifier
Char * C;

Here, * tells the computer that this is a pointer variable. The previous data type only indicates that the data of the pointer address is of that type. That is to say, C is a pointer, it's not Char (It's a nonsense ..).

As you can understand, '*' is equivalent to the 'int' when we define a variable int A = 3 ', the reason for declaring the memory data type (that is, why 'Char 'is required before' * ') is very simple. Different data types occupy different memory unit lengths, for example, if it is a short pointer, the memory occupies two bytes, but if it is long, it occupies four bytes. Therefore, there must be a data type, telling the pointer to take several bytes.

 

Thanks for the summary on the 8th floor. I will reference it here:

Pointer, which is a memory reader class.
The expected data value can be returned Based on the given data type definition and address location.

Array is a re-encapsulation of the pointer.
Contains a continuous memory block and a pointer constant.

 

About '*' and '&'

'*' Is a pointer operator. The name of '&' is the bitwise operator. The difference can be said as follows:

Int * P; // defines an int pointer.
Int;
P = & A; // P is equal to the address of a, that is, & this symbol indicates 'get address'

The '&' symbol can also be used as follows: Int & Y; to declare an int-type reference y.

 

Assign a pointerIt can be declared first, and then assigned a value, as shown in the preceding statement. It can also be directly assigned a value when declared. The effect is similar to that of normal variables. Note: For arrays, you can directly use the array name, because the array name is its address:

Int A [5];
Int * P = A; // '&' is not required, because a is an address.

If the pointer is used directly, the address is displayed and '*' is added. The content is displayed:

Int A = 5;
Int * P = &;
Cout <p <Endl; // This line outputs the address of.
Cout <* P <Endl; // This row outputs the number '5'

Note the following when declaring:

  • You can declare a pointer to a constant, but do not try to change this constant value's scalar (scalar _ scalar) scalar through a pointer. However, pointers are not as efficient as constants, A pointer can change the value of a specified object. As follows:

    Const char * P = "hello"; // This const is used to describe char
    Char s [] = "hi ";
    P = s; // This write is acceptable, which is equivalent to giving p a new address,

    * P = 'Hi'; // This is an error. It is said that char is a constant. Do not change it.

    // However:
    Char * P = "hello ";
    * P = 'H'; // This compilation is successful, but an error occurs. This is because the pointer rules are violated.

    This feature ensures that the constants pointed to by the pointer are not accidentally changed.

  • '*' Before const indicates that the pointer is a constant and cannot be changed. In this case, line2 is not allowed ~
    Char * const P = "ABC ";
    P = "def"; // Nonono. An error is required ~

     

  • Although I hate the rules, but programming must follow the rules to define what type of pointer, assign what type of value, you cannot assign 'x' to the int pointer, right. However, there are always a few rebellious, so there is a pointer called void, which can access any type of data through forced conversion. Void pointer usage:
    Void * VP; // a void pointer
    Int A = 5;
    Int * px;
    Vp = &;
    Px = (int *) vp; // force convert the void * of vp to int * to meet the px type.


Pointer operation:

Pointers can be added or subtracted, And the rules are easy to understand. For example, if I define a short pointer P, which occupies 4 bytes, then p + 1 is the number of 4 bytes after the address, which is generally used for arrays. For example:

Int A [] = {2, 3, 4, 5 };
Int * P =;
Cout <* (p + 1) <Endl; // The output is 3.

 

Pointer functions:

This is a big good thing. You can extend the return value from a large amount of data. The specific usage is to add "*" after the return type, which is very helpful. Let's also try something to avoid talking or not practicing it.

Int * getall (int * ){
.....
// After processing for half a day, return
Return;
}

(Thank you for your correction at @ Chen zihan (vczh)

In this way, four numbers instead of one number are returned. You can use the returned pointer to perform operations to obtain this batch of data and protect this batch of data.

 

Pointer to function:

In fact, functions are stored from the function name in the memory like arrays. That is to say, the function name is known, and then the function body is stored in the memory, since the pointer can point to an array, it can also point to a function. The form is: Data Type (* function pointer name) (Form parameter table) if the return value type of a function is = pointer data type parameter table = pointer parameter table, that function pointer can point to this function.

Void function (int A, int B );
Void function2 (int x, int y );
Void (* fpointer) (int, int); // It must be of the same type, number, and order as the function parameter to be directed.

// When used:
Function (5, 5 );
// The upper and lower sides have the same effect
Fpointer = function;
Fpointer (6, 6 );

Fpointer = function2; // you can point to other functions as long as the conditions are met.

Object Pointer(This part is actually useless)
It refers to the pointer syntax to a class: class name * pointer name; when calling a member: pointer name-> member name; note that before assigning a value to the pointer, You need to initialize this object ~

Clock c (1, 2, 4 );
Clock * p;
P = & c; // here c must be initialized. Otherwise, the pointer p cannot be used.
P-> getTime ();

Pointer to a non-static member of the class: When a pointer is defined outside the class to a public member of the class, the definition is as follows:

For public data members:

Clock clock (1, 1, 2 );
Clock * pClock = & clock;
// Define
Int Clock: * pHour;

// Value assignment:
PHour = & Clock: Hour;

// Call: (the three results are the same)
Clock. * pHour;
PClock-> Hour;
Clcok. Hour;

For public function members:

Clock clock (1, 1, 2 );
Clock * pClock = & clock;
// Definition: return type (Class Name: * pointer name) (parameter table)
Int (Clock: * pGetHour )();

// Value assignment:
PGetHour = & Clock: GetHour;

// Call: (the three results are the same)
Clock. * pGetHour;
PClock-> GetHour;
Clcok. GetHour;

I'm dizzy, but I don't know if my classmates are dizzy? If you get dizzy, You need to drool your mouth, brush your microblog first, and work and rest together ~

 

Dynamic Memory Allocation(Wow, it sounds good) seven lines of code are summarized:

Int * point;
Point = new int (2); // allocate dynamic storage space and put the value 2 in the memory
//.....
Delete point;
/*------------------------------------------*/
Clock * pclock = new Clock [2]; // Initialization is not allowed at this time
Pclock [0]. Set (1, 2, 3); // assume there is such a function.
Pclock [1]. Set (4, 5, 6 );
Delete [] pclock; // This is to say that '[] 'must be followed by 'delete'.

Shortest copy and deep copy(Last bit ~)

In fact, this code is complex, but it is easy to say.

If a member p in a class is a pointer, it defines the object clock and clock2, and p in clock points to a memory space with int data. When we only use clock2.p = clock. p is used to assign values to p in clock2. In fact, the pointer p of the two object objects points to the same memory space. In this way, when the number of this memory space is operated, it will affect both clock and clock2. this is a small copy.

Deep copy is to make it unimpressed. So if the p of clock2 is copied as follows: p = new int (* clock. p), then the two things do not affect each other. In this case, it is a deep copy.

 

Call, finally it's over. Play Weibo too ~

 

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.