Type parameter in C ++

Type parameter in C ++


Array

An array is a collection of data of the same type. The introduction of an array do not need to define a large number of variables in the program, significantly reduce the number of variables in the program, refining the program, but the array to be clear, easy to use, clearly reflects the links between data. Many good algorithms are associated with an array. Skillfully use the array, can greatly improve the efficiency of programming, to strengthen the readability of the program.

A memory cell array occupies in memory is contiguous, that is to say an array occupies a contiguous memory locations in memory. On 32-bit machines, the value of an int type accounted 4Byte, if a [0] to address 2000, the address then a [1] is 2004, a [2] of the address is 2008, a [3] addresses It is 2012 ...... and so on.

Difference strlen () and the sizeof () as follows:

  • strlen () is a function to calculate at run time. Character argument must be a pointer (char *), and must terminate in '\ 0' is. When the array name passed as an argument, in fact, it has been reduced to an array of pointers. Its function is to return the string length.
  • the sizeof () is an operator rather than a function, at compile time to calculate the number of bytes for data space. Thus, sizeof size can not be used to return the dynamically allocated memory space. sizeof commonly used in the return type and statically allocated objects, the space occupied by the array or structure, with the return value of the object, structure, content stored in the array does not matter.

 pointer

Data stored in the memory: If a variable is defined in the program, to allocate memory gave variable at compile time. The system in accordance with the type of variables defined in the program, a length of the space allocated. For example, C ++ compiler system 4Byte assigned to integer variables on 32-bit machines, 4Byte assigned to single precision floating point variable is assigned 1Byte character variable. Each byte memory area has a number, this number is the address. After the program has been compiled after converting the variable name for the address of a variable, the variable values ​​are accessed through the address.

This way the access address of the variable value by a variable called direct mode or direct access mode. It can also be employed another called indirect access (indirect access), the definition of a particular variable in a program, designed to store the address. Since the elements required can be found by the variable address, it can be said, the variable pointing to the address unit. Thus referred to visualize address "pointer", a pointer address of a variable is called the variable. If there is a special variable used to store the address of another variable (i.e., pointers), then it is called a pointer variable. Value of the pointer variable (i.e., the value stored in the pointer variable) is the address (i.e. pointer).

Also a pointer variable, Common variable is stored in the actual data, and a pointer variable contains the address in memory, this address points to a variable or a function. The contents of the pointer comprising: a type of the pointer type, pointer points to, and the pointer value of the pointer itself occupied memory area.

And an array of pointers:

An array of pointers , also known as row pointer, assumed is defined int (* p) [n] ; and () a high priority, a pointer p is first explained, and points to a one-dimensional array of integers. The length of the one-dimensional array is n, p can be said step size, that is performed when p + 1, p to cross the data length n integers.

Pointer array , int * p is defined assuming [n]; and [] high priority, may be understood first with p becomes an array, then the int * Description This is a pointer to an integer array, which has n pointer type the array elements. If this operation is performed p + 1 is wrong, p = a such an assignment is wrong, because p is agnostic representation, there is only p [0], p [1 ], p [2] ... p [ n-1], but they are pointer variables, the variables used to store the address only. But this assignment * p = a, where * p represents the value of the first element of the pointer array, the first address of A.

The difference between the two array pointers and pointer array:

  • Array pointer is just a pointer variable, can be considered a C language designed to point to a two-dimensional array, it takes a pointer in memory storage space;
  • Pointer array is a plurality of pointer variables, which is present in an array of memory, storage space occupied by the plurality of pointers. When also need to note that, while pointing to a two-dimensional array, and its direct references cited by the array name is the same.

String pointer:

(1) itself is a pointer variable string variable, for storing the first address string. When the pointer is defined, the compiler does not allocate space for the object pointer points, it is only allocated space pointer itself.

(2) the string itself is located in a contiguous memory address space of the first headed and treated with '\ 0' end mark as a string.

(3) Since the character array is composed of a plurality of array elements, each element of a character string stored. When defining a character array, a memory cell is allocated compiled, each element has the determined address.

Function pointer:

Function pointer variable is a pointer to function. Therefore, the function pointer is a first pointer variable, and this variable to a function. C ++ at compile time, each entry has an address of a function, the entry address is the function pointer points to the address. Once you have a pointer to a function variable, you can use the pointer variable is a function call.

Function pointer declared method is:

                                 Return Value (* pointer variable name) ([parameter list]);  

Wherein the return type of the function return value type described, (* pointer variable name) can not be omitted phrase parentheses.


Quote

It is a reference type variable, a variable which is used as an alias.

Method statement is quoted:

                          Type identifier reference name = & target variable name;

Definition of a reference r, which is a reference to a variable, i.e., the alias. After this statement, the role of a and r are the same, all represent the same variable. r and a cell to occupy the same memory, i.e., have the same address. When you declare a reference variable, you must initialize make that statement on behalf of which it is variable. During the execution of the function, can not be re-used as reference other variables.

An important role is cited as an argument to the function.


 

Examples cited as a parameter:

#include<iostream>

using namespace std;

void Mmin1(int a,int b)

{

int temp;

if(a>b)

{

temp=a;

a=b;

b=temp;

}

}

void Mmin2 (int & a, int & b) // reference parameter as a function of

{

int temp;

if(a>b)

{

temp=a;

a=b;

b=temp;

}

}

int main ()

{

int a=30,b=20;

Mmin1(a,b);

cout << a << "" << b << endl; // a, b values ​​remain unchanged.

Mmin2(a,b);

cout << a << "" << b << endl; // value of a is 20, b is a value of 30. a, b values ​​are modified

return 0;

}

        Results of the implementation are:

        30 20

20 30


The general function of the variable as a parameter, passed parameter is the value of the variable transmission is unidirectional. If during the execution of the function of the parameter value changes, does not return to the argument. Because when calling the function, parameters, and arguments are not the same memory cell.

When using a reference transfer function of the parameters, it did not produce a copy of the argument in memory, but to direct the operation argument. Usually when a variable transmission function of the parameters, when the function call occurs, it is necessary to allocate storage unit parameter, the variable parameter is a copy of the argument variables; if the object is passed, will call the copy constructor. Thus, when the transmission parameters of data is large, more efficient than with reference to the parameters passed by a general variable, less the space occupied.

Use a pointer as a parameter, although it can achieve the same effect with the use of references, but give the same parameter assignment unit called function memory, and the need to reuse in the form of "* pointer variable name" operation is performed, it is very easy reading error and poor procedures; on the other hand, at the point of calling the calling function, the address of the variable to be used as an argument, which is not very convenient.

Often quoted

If we want to improve the efficiency of the program, but also the data passed to the function is not the function is changed, you should use a constant reference.

Declaratively often cited are:

                             const type identifier reference name = & certain variable names;

In this way a statement of reference, not by reference to the value of the target variable is modified, the target references in the program to be const type manipulation, thereby ensuring the safety references.

 

Guess you like

Origin www.cnblogs.com/xiongweiLi/p/12345622.html