[英]Simple C++ pointer confusion
void changeStr(char *str)
{
str = "D";
}
void changeStr(char **str)
{
*str = "S";
}
char str[] = "Good";
changeStr(str);
cout<<str<<endl;
char *p = str;
//*p = 'j';
changeStr(&p);
cout<<str<<endl;
I just try to change the value of str[]
that array. 我只是尝试更改str[]
该数组的值。 WITHOUT RETURN! 没有退货!
I think the first changeStr
just pass in pointer of str
, and change that value, but actually it did not change it. 我认为第一个changeStr
只是传入str
指针,并更改了该值,但实际上并没有改变它。
The second I use pointer of pointer but also cannot work. 第二个我使用指针的指针,但也无法正常工作。
Let's take it step by step. 让我们逐步进行。
char str[] = "Good";
You are creating an array of characters, 5 characters long with the following content: 您正在创建一个字符数组,长度为5个字符,内容如下:
{ 'G', 'o', 'o', 'd', '\\0' }
changeStr(str);
Here, you are passing that array to a function. 在这里,您正在将该数组传递给函数。 Since arrays decay into pointers, this call is perfectly OK. 由于数组会衰减为指针,因此此调用完全可以。
void changeStr(char *str)
{
str = "D";
}
Now, here comes the first issue. 现在,这是第一个问题。 You are probably confusing "D"
and 'D'
. 您可能会混淆"D"
和'D'
。 If you want to change the first character in the array you need to do it the following way: 如果要更改数组中的第一个字符,则需要采用以下方法:
str[0] = 'D';
This would work fine. 这样就可以了。 Changing the pointer won't do anything, because it's a local variable that holds a pointer to the beginning of the array, not the array itself. 更改指针不会执行任何操作,因为它是一个本地变量,该指针持有指向数组开头的指针,而不是数组本身。 If you want to replace the entire content of the array with just { 'D', '\\0' }
, you would need to use strcpy
. 如果只用{ 'D', '\\0' }
替换数组的整个内容,则需要使用strcpy
。
strcpy(str,"D");
Now, let's check the last part. 现在,让我们检查最后一部分。 Here you mix things up a bit. 在这里,您将事情弄混了。
char *p = str;
changeStr(&p);
You are creating a new variable that points to the beginning of the array and you pass pointer to that variable into the next function. 您正在创建一个指向数组开头的新变量,并将指向该变量的指针传递给下一个函数。
void changeStr(char **str)
{
*str = "S";
}
Which does indeed change the original variable passed, but remember, this is p
not the array. 这确实改变传递的原始变量,但请记住,这是p
不是数组。 What you did is change where p
points. 你所做的就是改变p
指向的位置。 It now points to a constant "S"
. 现在它指向常数"S"
。
In the first version of the changeStr
function, the argument str
is considered a local variable, and as all local variables changes to them are only changed inside the actual function. 在changeStr
函数的第一个版本中,参数str
被视为局部变量,并且由于对它们的所有局部变量的更改仅在实际函数内部进行了更改。
The second version passes the string as a reference. 第二个版本将字符串作为参考传递。 Basically this is what happens when you specify that a variable should be passed as reference with the &
specification: 基本上,这是当您指定应使用&
规范将变量作为引用传递时发生的情况:
void changeStr(char *&str)
Internally the compiler passes references as pointers. 在内部,编译器将引用作为指针传递。
For the value of str to change you have to pass it by reference. 要更改str的值,您必须通过引用将其传递。
It must be like 一定像
void changeStr(char* &str)
this will ensure that variable is passed by reference, thus if you make any changes to the variable in the function it will be a global change. 这将确保变量通过引用传递,因此,如果您对函数中的变量进行任何更改,它将是全局更改。
you shouldn't use assignment operator to store string in a variable even if its a pointer. 您不应该使用赋值运算符将字符串存储在变量中,即使它是指针。 use strcpy after including string.h 在包含string.h之后使用strcpy
void changeStr(char *str)
{
strcpy(str, "D");
}
this should change the data to which str pointer is pointing...even in the scope where the function call was made... 这应该更改str指针指向的数据...甚至在进行函数调用的范围内...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.