当前位置:网站首页>Review of arrays and pointers triggered by a topic

Review of arrays and pointers triggered by a topic

2022-06-25 14:51:00 -Ximen blowing snow

Catalog

subject

  Their thinking

Array review practice

Pointer review practice


subject

  int a[2][2][3] = {
   {
   {1, 2, 3}, {4, 5, 6}}, {
   {7, 8, 9}, {10, 11, 12}}};
 
  int *ptr = reinterpret_cast<int*>(&a + 1);
  qDebug() << *(reinterpret_cast<int*>(a + 1)) << *(ptr - 1);///> Output 7 12

  The output is 7 12

  Their thinking

&a+i = a+i*sizeof(a);
a+i = a+i*sizeof(a[0]);

Reference resources

C++ primer 6 chart
C++primer plus 6

Array review practice

Dip a few pictures

C++primer plus 6
C++primer plus 6
C++primer plus 6
C++primer plus 6

Under test

//--------------------------- Array 
  // Array initialization 
  int crd[4] = {4, 4, 4, 4}; ///>ok
  int crd1[3];///>ok

// 1-  Non assignable error :error: array type 'int [3]' is not assignable, Non assignable 
//  crd1 = {3,3,3};
//  crd1 = crd;
  // Partial initialization 
  double tips[5] = {3.44, 5.2};///> Partial initialization , Other settings are 0
  qDebug() << tips[0] << tips[2]; ///> Output 3.44 0
  //[] It's empty time , The compiler calculates the size of the array , But this is a very bad practice , But this is better when the character array is initialized to a string 
  short tips2[] = {2, 3, 4, 5, 5, 5, 5, 5};
  qDebug() << sizeof (tips2);///> Output 16 = 2*8
  //c++11 Initializing the array 
  //1- Omit the equal sign 
  double ee[4] {1.2, 2.3, 3, 3};
  //2-{} It doesn't contain anything 
  double ee1[4] = {};///> Initialize to 0
  double ee2[4] {};
  //3- List initialization base narrowing conversion , Insert an explicit cast to eliminate this problem 
  //  long ee3[4]{1.2,2.3,3,3};///>error: type 'double' cannot be narrowed to 'long' in initializer list
  char ee4[3] {11, 22, '1'}; ///>ok
  // Character array initialization 
  char str[3] = {'2', '2', '3'}; ///> Not a string 
  char str1[3] = {'2', '2', '\0'}; ///> Is a string 
  // Literal constant initialization 
  char str2[] = "123456";
//  char str3[7] = "1234567";///> error: initializer-string for char array is too long
  char str4[7] = "123456";
  qDebug() << sizeof (str2) << sizeof (str4);///> Output  7 7
  //  "123456" What is actually identified is the memory address where the string is located 
  //  char addr = "123";///>error: cannot initialize a variable of type 'char' with an lvalue of type 'const char [4]'

Pointer review practice

Dip chart

C++primer plus 6 Initialization of the pointer
C++primer plus 6 Declaration of function pointer
The use of function pointers

Under test


  int i1 = 6;
  long long i2 = 3;
  quint64 i3 = 333;
  int *p = &i1;
  long long *p1 = (static_cast<long long *>(&i2));
  quint64 *p2 = &i3;
  qDebug() << sizeof (p) << sizeof (p1) << sizeof (p2);///> Output :4 4 4
  int *p3, p4; ///>p3 It's a pointer ,p4 It's a int
  //1  A function pointer 
  double pf1(int);///> function 
  double (*pf)(int);///> A function pointer ,pf Is a pointer to a function 
 //  double*pf1(int)///> A return value is double The script of the pointer , It is not allowed to define a function here 
  pf = pf1;///>pf Is a pointer pf1() The pointer ; notes :pf1() The return value and parameters of the function must be the same as pf Same type of 
  double pf2(double);
  /**
    error: assigning to 'double (*)(int)' from incompatible type 'double (double)':
    type mismatch at 1st parameter ('int' vs 'double')
  */
  //pf = pf2;
  //2  Call a function... Using a function pointer , Either way 
  double vv = pf(3);///> Use the function name 
  double vv1 = (*pf)(4);///> Use function pointers , This way is clear 
  //3  More complex function pointers 
  //3-1  The characteristic values and return values of the following functions are the same 
  const double * f1(const double ar[],int n);
  const double * f2(const double [],int n);
  const double * f3(const double *,int n);
  //3-2  Declare a function pointer 
  //3-2-1
  const double * (*f4)(const double ar[],int n);///> First of all 
  f4 = f1; f4 = f2; f4 = f3;
  //3-2-2
  const double * (*f5)(const double ar[],int n) = f1;///> Initialize when declared 
  //3-2-3 C++ Infer for yourself 
  auto f6 = f1;
  //3-2-4  Declare an array of three pointers ,[] Has a higher priority than * ,*f7[3] Is an array of three function pointers ,(*f7)[3], So it's a pointer , An array that points to three elements 
  const double * (*f7[3])(const double ar[],int n) = {f1,f2,f3};///> Initialize when declared 
  //3-2-4-2  The above situation cannot be used auto,auto Can only be used for single valued initialization 
  auto f8 = f7;
  //3-2-4-3  Call function 
  double aa[] = {1.1,2.2,3.3};
  const double * ppx = f7[0](aa,3);
  const double * ppx1 = (*f8[1])(aa,3);
  //3-2-4-4  Get directions double Value 
  double ppxv = *f7[0](aa,3);
  double ppx1v = *(*f8[1])(aa,3);
  /**
     Be careful :*f7[3] in  f7 yes &f7[0],&f7 Is the address of the entire array ( An address containing three pointers ),f7+1 Is the next element in the array ,&f7+1 It is sizeof(f7)+f7
  */

 

原网站

版权声明
本文为[-Ximen blowing snow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200518257975.html