简体   繁体   中英

Changing the value of a string with a pointer in C

I was trying to change the value of the string t_string by passing a pointer of the string to a function and then changing the value of that pointer in the function.

The output I am receiving is the original string "Hello".

Any help would be greatly appreciated :) Thanks!

#include <stdio.h>
#include "string.h"

void editString(char *theString);

int main() {
    char t_string[] = "Hello\n";
    char *s = t_string;
    editString(s);
    printf("The string after: %s", t_string);
    return 0;
}

void editString(char *theString){
    theString = "Good Bye\n";
}

In C, parameters are pass by value. What you're doing is changing the value of the local variable theString to point to a string literal. That local variable change is not reflected in the calling function.

To change what theString points to, use strcpy . That will follow what theString points to (ie the array in main ). Note however that the array isn't large enough to hold the new string, so if you do that you will write past the end of the array and invoke undefined behavior. So you need to make the array large enough to hold either string.

int main() {
    char t_string[20] = "Hello\n";   // enlarge array to hold either string
    char *s = t_string;
    editString(s);
    printf("The string after: %s", t_string);
    return 0;
}

void editString(char *theString){
    strcpy(theString, "Good Bye\n");   // use strcpy
}

Array designators are non-modifiable lvalues. So you can not change an array such a way.

In this statement

char *s = t_string;

there is created a new object with the name s that is initialized by the address of the first character of the array t_string .

In this call

editString(s);

there is created a copy of the value of the argument that is assigned to the function parameter.

You can imagine the function definition and its call the following way

editString(s);

//...

void editString(/*char *theString*/){
    char *theString = s; 
    theString = "Good Bye\n";
}

So even the argument s is not changed by the function. That is at first the local variable theString initialized by the value stored in the argument s and then this local variable is reassigned by the address of the first character of the string literal "Good Bye\\n" .

Neither the array t_string nor the pointer s defined in main were changed in the function.

If you want to change the array you have to assign its characters individually for example by using the standard string function strcpy declared in header <string.h> . The array has to have enough size to accommodate the new string literal.

For example

#include <stdio.h>
#include "string.h"

void editString(char *theString);

int main( void ) 
{
    char t_string[10] = "Hello\n";
    char *s = t_string;
    editString(s);
    printf("The string after: %s", t_string);
    return 0;
}

void editString(char *theString){
    strcpy( theString, "Good Bye\n" );
}

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )

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