• 二级指针 杂记


    二级指针与数组

    二级指针与数组的使用—混淆点

    std::cout 与char*

    指针和数组的特殊关系可以扩展到c-风格字符串。请看下面的代码
    char flower[10] = “rose”;
    cout << flower << “s are red\n”;
    数组名是第一个元素的地址,因此cout语句中的flower是字符‘r’的地址。cout对象认为char的地址是字符串的地址,因此他打印带地址处的的字符,然后继续打印后面的字符,直到遇到‘\0’为止。总之,如果提供一个字符的地址给cout,它将从该字符开始打印,直到"\0"为止。

    char a[5] = {'h','e','\0','l','l'};
    only print  "he"
    
    • 1
    • 2

    cannot convert ‘char (*)[2]’ to ‘char**’ in assignmentchar

    error1: char[2]

    char** p = nullptr;
    char a[2] = {'q','\0'};
    p = &a; // this cause error, cannot convert 'char (*)[2]' to 'char**' in assignment
    
    • 1
    • 2
    • 3

    这个错误是因为,对于数组a, a本身就表示数组的首地址,与&a的得到的结果是一样的。&a 并不是得到指向a pointer的地址, 还是a的地址.
    p= a; // error cannot convert ‘char [2]’ to ‘char**’ in assignment

    真想要**, 必须要 数组指针

    error2 string to char

    void tower(int n,char a,char b,char c);
    tower(4,"A","B","C");
    
    • 1
    • 2

    error3

    while (cDriveLetter <= 'Z')
    	{
    		if (0 < (dwDrivemap & 0x00000001L))
    		{
    		  cDriveLetter;
    		/*  char *p;
    		  p = new char[1];
    		  p= cDriveLetter;*/
    		   char arr[] = cDriveLetter;
    		  if (getfil((void*)arr) == 0)
    		  {
    			  getfil((void*)arr);
    		  }
    			//printf("%c:\\\n", cDriveLetter);
    			//return cDriveLetter;
    		  else
    			  cDriveLetter++;
    		}
    		cDriveLetter++;
    		dwDrivemap = dwDrivemap >> 1;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    solution

    char arr[] = &cDriveletter;
    or
    char arr[2];
    arr[0] = cDriveLetter;
    arr[1] = '\0';
    
    • 1
    • 2
    • 3
    • 4
    • 5

    char[] 是一种特定的类型,可以copy assign to char*, 但是反过来就会出现编译问题

    class Student {
     char name[20];
     long number;
    public:
     Student (char nm[20], long val) : 
          name(nm), number(val) {}
          an error saying incompatible types in assignment of char* to char[20] 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Link

    the error message is telling you that you cannot assign a pointer to an array. Fair enough. Doesn’t really matter anyway, since the language also doesn’t let you assign arrays to arrays.

    改变另外array的方法,只有copy数据到那里。

    二级指针 int ** 改变值

    在这里插入图片描述

    static int number = 99;
    
    void func(int** b);
    
    int* b = nullptr;  // empty 
    func(&b);
    cout << *b << endl; // b will have value
    
    void func(int** b)
    {
    	// **b 是二级指针,我们关注的是他指向的内容。
    	// 它的内容是一个指针,我们希望这个指针指向一个新的对象的地址
    	*b = &number;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    总结

    当我们将指针作为函数的参数时候,我们在乎的是指针指向的内容。
    如果不改变,我们需要用const修饰,如果改变,则不需要const。
    当我们关注点是 改变指针所指的对象时,单纯的copy这个指针是解决不了问题的,因为copy的是指针指向的对象地址,并不是这个指针。
    如果我们相对指针进行改变(指针的指向),必须要对这个指针取地址,才能操作其内容

    二级指针与数组

    当二级指针作为参数时,更多的情况下,他是一个指针数组。
    这里涉及2点

    • 如何把定义的int* a[3] 转为参数 int** 传给函数
    • 如何销毁数组对象
    void func(int** b);
    
    // define array
    int* test[3];
    
    // pass it to func 2
    func(test);        // the index
    // pass it to func 2
    funct(&test[0]);   // first number's index
    // pass it to func3
    int** casted = static_cast(test);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    定义在栈上的数组,我们不需要删除。但是new出来的东西是需要删除的。
    测试代码

    class MyClass
    {
        public:
        MyClass(int a): a(a)
        {
            cout << "MyClass(int a): " << this->a << "\n" ;
        }
    
        MyClass(): a(0)
        {
            cout << "MyClass(): \n";
        }
    
        ~MyClass()
        {
            cout <<"~MyClass() \n";
        }
    
        int a ;
    };
    void factoryArray(MyClass** p, int num = 2)
    {
        // at the next step, turn MyClass** to MyClass[]
        p[0] = new MyClass(0);
        p[1] = new MyClass(1);
        p[2] = new MyClass(2);
    }
    int main()
    {
    
        MyClass* MC3[3];    // 放Myclass* 的数组
        factoryArray(&MC3[0]);  // MC3S数组的第一位的地址,这么输入是正确的 MC3 MC3[0]
        cout << MC3[0]->a <<"\n";
        cout << MC3[1]->a <<"\n";
        cout << MC3[2]->a <<"\n";
    
        for(int i=0;i<3;i++)
        {
            delete MC3[i];
        }
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    打印信息是:
    MyClass(int a): 0
    MyClass(int a): 1
    MyClass(int a): 2
    0
    1
    2
    ~MyClass() 
    ~MyClass() 
    ~MyClass() 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    debian和ubuntu的核心系统和系统命令的区别
    使用小程序制作一个老照片修复工具,让追忆时光触手可及
    单向链表(Singly Linked List)
    leetcode 609. Find Duplicate File in System(找到相同的文件)
    springboot+java海洋馆门票预订网上商城线上销售系统
    flinkcdc踩坑指南
    R语言贝叶斯广义线性混合(多层次/水平/嵌套)模型GLMM、逻辑回归分析教育留级影响因素数据...
    GPS定位系统,GPSBDpro-远程车辆视频录像回放
    CF547E Mike and Friends (AC 自动机+树状数组)
    【英雄哥六月集训】第 23天: 字典树
  • 原文地址:https://blog.csdn.net/tortelee/article/details/127830865