简体   繁体   中英

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. WITHOUT RETURN!

I think the first changeStr just pass in pointer of str , and change that value, but actually it did not change it.

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:

{ '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' . 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 .

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. What you did is change where p points. It now points to a constant "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.

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.

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM