1. Two-dimensional arrays

Two-dimensional arrays can be classified as dynamic and static.

  1. The size of the static two-dimensional array is allocated during compilation, and its memory is automatically released by the computer after use, with high efficiency.
  2. The dynamic array is dynamically applied from the heap memory by the programmer according to the actual needs when the program is running, and released by the programmer after use, which is inefficient.
// Static two-dimensional array, allocated on the stack.
	const size_t n = 2;// size_t is an unsigned int
	int arr[n][10];//
	for (size_t i = 0; i < n; i++) {
		for (size_t j = 0; j < n; j++)
			arr[i][j] = i * n + j;
	}

Copy the code
// Dynamic array
// The left side of the equation declares a pointer to pchar, pointing to the type of char[10], and the right side of the equation assigns a block of n*10*sizeof(char) bytes to pchar. New returns a pointer to the first address of the block
	char (*pchar)[10] = new char[n][10];
	
// You can also write it this way
    const int n = 2;
    int** arr = new int* [n];//arr is a pointer to a pointer that allocates an array of n elements
    for (int i = 0; i < n; i++) {
        arr[i] = new int[10];
    }
    / / assignment
    for (int i = 0; i < n; i++)
        for (int j = 0; j < 10; j++)
            arr[i][j] = i*10+j;
    // Free memory
    for (int i = 0; i < n; i++)
        delete[] arr[i];
    delete[] arr;
Copy the code

2. Overloading

int add( int a, int b )
{
    return a+b;
}

int add( int a, int b, int c=0 )
{
    return a+b+c;
}

int main( int argc, char **argv )
{
    // int value = add(1,2); // The compiler cannot determine which function to call because the second function uses default arguments, although the two functions have different numbers of arguments
    return 0;
}
Copy the code

3. Pointers and references

A pointer to a reference cannot be defined. But there are references to Pointers:

int i = 42;
int *p;
int *&r = p;
Copy the code

The easiest way to understand what r’s type really is is to read the definition of R from right to left. The symbol (&) closest to a variable has the most direct effect on its type, so r is a reference. The rest of the declarator determines what type r refers to, and * indicates that it refers to a pointer. Finally, the basic data type section of the declaration indicates that r refers to an int pointer.

int &*r; // ERR:pointer to reference is not allowed.Copy the code

* Pointer to reference is not allowed. The following program uses Pointers and references to exchange values of two variables

#include <iostream>

void swap_ref( int & a, int & b )
{
    int c = a;
    a = b;
    b = c;
}

void swap_ptr( int * a, int * b )
{
    int c = *a;
    *a = *b;
    *b = c;
}
// Swap the values of two pointer variables by referencing them
void swap_ref_ptr( int * & a, int * & b )
{
    int *c = a;
    a = b;
    b = c;
}

int main( int argc, char **argv )
{
    int i1 = 1;
    int i2 = 2;
    int * p1 = &i1;
    int * p2 = &i2;

    std: :cout << "i1 = " << i1 << "," << "i2 = " << i2 << std: :endl;
    swap_ref(i1,i2);
    std: :cout << "i1 = " << i1 << "," << "i2 = " << i2 << std: :endl;
    std: :cout << std: :endl;

    std: :cout << "*p1 = " << *p1 << "," << "*p2 = " << *p2 << std: :endl;
    swap_ptr(p1,p2);
    std: :cout << "*p1 = " << *p1 << "," << "*p2 = " << *p2 << std: :endl;
    std: :cout << std: :endl;

    std: :cout << "*p1 = " << *p1 << "," << "*p2 = " << *p2 << std: :endl;
    swap_ref_ptr(p1,p2);
    std: :cout << "*p1 = " << *p1 << "," << "*p2 = " << *p2 << std: :endl;

    return 0;
}
Copy the code

4. The assertion Assert

Declaration: void assert (int expression);

Expression can be any valid C expression in most cases. In the following program, we divide or calculate a/B (where a and B are integers), and you know that B cannot be zero, so we use assert (b! = 0). If the condition (B! = 0), the program will continue to execute. Otherwise, it terminates and displays an error message on the screen specifying the file name, line number, function name, and condition that is not true

#include <stdio.h>
#include <assert.h>
 
int main(a) {
  int a, b;
 
  printf("Input two integers to divide\n");
  scanf("%d%d", &a, &b); assert(b ! =0);
 
  printf("%d/%d = %.2f\n", a, b, a/(float)b);
 
  return 0;
}
Copy the code