Introduction to C + + Step03 [string and pointer]

Posted by jannoy on Thu, 20 Jan 2022 06:31:06 +0100

Common string handling functions:

  1. String concatenation function: (strcat)

    char * strcat (char destination[], const char source[]);
    //Function: connect the string of parameter 2 to the end of the first parameter string
    //Return value: the first address of the first string
    //Note: ensure that the space length of parameter 1 is enough to store the string length of parameters 1 and 2, otherwise the memory overflows
    
    //For example, chestnuts:
    char des[50] = "hello "; //Ensure that the space of des is large enough
    char src[] = "cctry.com";
    strcat(des, src);
    cout << "des = " << des << endl;	//des = hello cctry.com
    
  2. String copy function: (strcpy)

    char * strcpy( char destination[], const char source[]);
    //Function: copy the string of the second parameter to the character array of the first parameter
    //Return value: the first address of the first string
    //Note: ensure that the character array size of the first parameter is sufficient; The terminator '\ 0' of the second parameter will also be copied.
    
    //For example, chestnuts:
    char des[50] = {0};
    char src[] = "cctry.com";
    strcpy(des, src);
    cout << "des = " << des << endl;	//des = ccrty.com
    
  3. String comparison function: (strcmp)

    int strcmp (const char str1[], const char str2[]);
    //Function: compare the character array strings of the first and second parameters, and compare them letter by letter until the end of the string
    //Return value: when the first parameter is greater than the second parameter, the number > 0 is returned; when the first parameter is less than the second parameter, the number < 0 is returned; when the first parameter and the second parameter are equal, 0 is returned
    
    //Take chestnuts for example:
    char des[50] = "hello";
    char src[] = "cctry.com";
    int iret = strcmp(des, src);
    cout << "iret = " << iret << endl;	//iret = 1
    
  4. String length function: (strlen)

    size_t strlen (const char str[]);
    //Function: get the length of parameter string
    //Return value: returns the length of the string
    
    //For example, chestnuts:
    char des[50] = "hello";
    int len = strlen(des);
    cout << "len = " << len << endl;	//len = 5
    
  5. The difference between the length of the string and the number of bytes occupied:

    1. Take chestnuts for example:

      char des[50] = "Hello";
      //String length strlen()
      int len = strlen(des);	//len = 5
      //Occupied bytes
      int size = sizeof(des);	//size = 50
      
    2. Chestnut Description:

      1. String length:

        Only the character length from the beginning of the string to the occurrence of the first '\ 0'.

      2. Occupied bytes:

        It refers to the byte size of the occupied memory. It is applied, that is, the memory has nothing stored, and the memory is still occupied.

Address and pointer:

  1. Address: the place where the variable is stored, and the address of the variable

  2. Pointer: a special type variable used to store addresses

    char* p;	//To define what type of pointer, add a * after the type to represent the meaning of pointer type variable
    int a = 5;	//The content of variable a is 5; 	& A is the address of variable a
    int* pa = &a;	//'&' is the address character. Here is the address of 'a'
    /*Pay attention to understanding:
    Pointer is also a variable, but it stores an address, which is not very special;
    Since it is a variable, it has the same content as "int a = 5" (it stores the address of a - & A) and the address (the address (place) of the variable pa itself) & pa);
    */
    

    Pointer variables are also variables. They also have content and address. The values or contents of other types of variables reflect themselves. However, the value or content in the pointer variable reflects other variables.

  3. Variables and pointers:

    1. Variable ----- > address:

      int a = 5;
      int* pa = &a;	//& store the address of a in the pointer variable pa
      
    2. Address ----- > variable:

      int a = 5;
      int* pa = &a;
      int b = *pa;	//Value the address stored in pa and assign it to b;
      

    explain:

    Add a * in front of the pointer variable to get the variable itself pointed to by the pointer.
    Therefore, the modification of a variable can be realized either through the variable itself (direct modification) or through the pointer / address (indirect modification).

  4. Definition and initialization of pointer variables:

    ① The definition format of pointer variable is as follows:

    Base type * pointer variable name;
    ② The symbol * can be close to either the base type or the pointer variable name, for example:
    ​ int* p; And int *p; All right.
    ③ Pointer variables can be initialized at the time of definition, defined first and then initialized, or point to the address of a variable at any time in the future:

    int a = 5;
    int *pa;
    pa = &a;
    int *pb = &a;
    

    ④ The base type is the type of the variable that the pointer variable points to. For example:
    int* pa; The definition of this pointer variable is to define a pointer variable PA pointing to int type; You can't give him the address of a variable of type float. For example:
    int* pa;
    float a = 2.6;
    pa = &a;
    This is wrong.
    ⑤ Pointer variables can point to variables of the same type, for example:

    int a = 5, b = 6;
    int *p = &a;
    p = &b;
    

    That is, the pointer variable p can point to both the address of variable a and the address of variable b.

    That is, the pointer variable p can point to both the address of variable a and the address of variable b.

  5. practice:

    1. Exchange the values of two parameters:

      #include<iostream>
      using namespace std;
      
      void swap(int* pa ,int* pb)
      {
          *pa = *pa+*pb;
          *pb = *pa-*pb;
          *pa = *pa-*pb;
      }
      
      int main()
      {
          int a = 1 ,b = 2;
          swap(&a ,&b);
          return 0;
      }
      
    2. Implement a function to obtain the maximum and minimum values in an array. The function declaration is as follows:
      void get_min_max(int src[], int* max_v, int* min_v)
      {
      //The function code is written below this
      }

      #include<iostream>
      using namespace std;
      
      void get_min_max(int src[], int* max_v, int* min_v)
      {
          int size_ary = sizeof(src);
          for(int i=0;i<size_ary;i++)
          {
              *max_v = src[i] > *max_v ? src[i] : *max_v;
              *min_v = src[i] < *min_v ? src[i] : *min_v;
          }
      }
      int main()
      {
          int src[] = {1,4,5,9,3,0,-6};
          int max_v = src[0] ,min_v = src[0];
          get_min_max(src, &max_v, &min_v);
          cout << "The max of the array is : " <<max_v<<"\r\nThe min of the array is : "<<min_v <<endl;
          return 0;
      }
      

Topics: C++ pointer string