简体   繁体   English

用C中的指针更改字符串的值

[英]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. 我试图通过将字符串的指针传递给函数,然后在函数中更改该指针的值来更改字符串t_string的值。

The output I am receiving is the original string "Hello". 我收到的输出是原始字符串“ 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. 在C语言中,参数按值传递。 What you're doing is changing the value of the local variable theString to point to a string literal. 您正在做的是更改局部变量theString的值以指向字符串文字。 That local variable change is not reflected in the calling function. 局部变量的变化不会反映在调用函数中。

To change what theString points to, use strcpy . 要更改theString指向的内容,请使用strcpy That will follow what theString points to (ie the array in main ). 这将遵循theString指向的内容(即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 . 创建了一个名为s的新对象,该对象由数组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. 因此,即使参数s也不会被函数更改。 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" . 首先是由存储在参数s的值初始化的局部变量theString ,然后由字符串文字"Good Bye\\n"的第一个字符的地址重新分配此局部变量。

Neither the array t_string nor the pointer s defined in main were changed in the function. t_string未更改数组t_string或main中定义的指针s

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> . 如果要更改数组,则必须单独分配其字符,例如,使用在标题<string.h>声明的标准字符串函数strcpy 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 考虑到根据C标准,没有参数的函数main应该声明为

int main( void )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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