二级指针与数组的使用—混淆点
指针和数组的特殊关系可以扩展到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"
error1: char[2]
char** p = nullptr;
char a[2] = {'q','\0'};
p = &a; // this cause error, cannot convert 'char (*)[2]' to 'char**' in assignment
这个错误是因为,对于数组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");
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;
}
solution
char arr[] = &cDriveletter;
or
char arr[2];
arr[0] = cDriveLetter;
arr[1] = '\0';
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]
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数据到那里。

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;
}
当我们将指针作为函数的参数时候,我们在乎的是指针指向的内容。
如果不改变,我们需要用const修饰,如果改变,则不需要const。
当我们关注点是 改变指针所指的对象时,单纯的copy这个指针是解决不了问题的,因为copy的是指针指向的对象地址,并不是这个指针。
如果我们相对指针进行改变(指针的指向),必须要对这个指针取地址,才能操作其内容
当二级指针作为参数时,更多的情况下,他是一个指针数组。
这里涉及2点
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);
定义在栈上的数组,我们不需要删除。但是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;
}
打印信息是:
MyClass(int a): 0
MyClass(int a): 1
MyClass(int a): 2
0
1
2
~MyClass()
~MyClass()
~MyClass()