7-C + + programming module

Posted by sholtzrevtek on Sat, 20 Nov 2021 18:03:14 +0100

c + + programming module

In the function header or prototype parameter, int arr [] is equivalent to in t *arr

Because the array name can be regarded as a pointer, the array name is interpreted as the address of the first element

arr[i] == *(arr+i)

&arr[i] = arr + i

The above advantages of passing by array or pointer:

  • Save the time cost of replication;
  • Save space cost caused by copying;

The disadvantages are:

  • Using raw data increases the risk of breaking words

To tell the array handler the array type and the number of elements, pass them through two different parameters

void filer(int arr[], int size) is better than void file(int arr[size])

const and pointer

const is used for pointers in two ways:

  • Let the pointer point to a constant: prevent the pointer from modifying the value pointed to;
  • The pointer itself is declared as a constant: prevent changing the value pointed to by the pointer;
// The pointer points to a constant, const int
age = 30
const int * pt = &age

The above cannot be modified with pt pointer.

int sloth = 3
const int * ps = &sloth  // a pointer to const int
int * const finger = &sloth  // a const pointer to int

Use const as much as possible

There are two reasons to declare a pointer parameter as a pointer to constant data:

  • Avoid programming errors caused by inadvertent modification of data
  • Using const enables the function to handle const real and participate in non const arguments, otherwise it can only accept non const data

If conditions permit, the pointer parameter should be declared as a pointer to const

Functions and 2D arrays


int sum(int (*arr)[4], int size)
int sum(int arr[][4], int size)
int data[3][4] = {{1,2,3,4}, {9,8,7,6}, {2,4,6,8}}
int total = sum(data, 3)  // Number of rows as a parameter

The array name is equivalent to the starting address of the array

  • arr refers to the pointer address of the first element of the array and the pointer to the four elements in the first row

  • arr+r refers to the pointer address of the element numbered r, and the pointer of the fourth element on line r

  • *(arr+r) data in row r

  • arr[r][c] value of row r and column c

  • *(arr+r) + c: pointer to the data in row r and column c, which is equivalent to arr[2] + c

  • *(* (arr+r)) + c: data in row r and column c, equivalent to arr[r][c]

Functions and c-style strings

The name of the string represents the address of the first element of the string

ch = "helloworld"

*ch --> 'h'

char * pstr = new char [n+1]

del [] pstr
while(n-- > 0)


int i=0;

Better, save the use of additional variables

Functions and structures

  • When the structure is relatively small, the value transfer structure is the most reasonable
  • When the structure is relatively large, it is most reasonable to pass according to the pointer
  • You can also pass by reference
While(cin >> struct.x >> struct.y)  // If you can, you can add an error condition to prohibit further reading

} // If the program needs input after the input cycle, the input must be reset using cin.clear()

Understand the string and array series, and master the characters, strings and arrays

void show(const rect *pxy)  // void show(const rect *pxy) of rectangular coordinate system / / pointer mode of rectangular coordinate system

void show(const rect pxy)  // void show(const rect *pxy) of rectangular coordinate system / / pointer mode of rectangular coordinate system



string list[5];
geline(cin, list[i]);



const seasons = 4
const array<string, seasons> Snames={"spring", "summer", "fall", "winter"};
const array<double, Seasons> *pa;
cin>> (*pa)[i]
cout <<(*pa)[i]


c + + does not allow main() to call itself

Function pointer

  • Get function address

Pointer to the function returned by process(think)

Process(think()) returns a value

  • Declare function pointer

    double pam(int)

    double (*pf)(int)

  • Use a pointer to call a function

    void estimate(int line, double (*pf)(int))

    double x = (*pf)(5)

    double x = pf(5) // pf is equivalent to (* pf)

Topics: C++