C语言 让函数更改指针在 C 中表示的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4844914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Having a function change the value a pointer represents in C
提问by Nick
I have a mainfunction that has a char, I am attempting to pass a pointer to that charinto a function and have it change it from Ato Bbut it just doesn't seem to change it. The example shown here is just the current state of the code I have tried many different variations on it thus there may be other mistakes in there from simply clutching at straws.
我有一个main带有字符的函数,我试图将指向该字符的指针传递给char一个函数并将其从Ato更改为B但它似乎并没有更改它。这里显示的示例只是代码的当前状态,我已经尝试了许多不同的变体,因此可能存在其他错误,因为只是抓住稻草。
int main()
{
char result = 'A';
setChar(&result);
printf("%C", result);
}
void setChar(char* charToChange)
{
charToChange = "B";
}
回答by Jeremiah Willcock
What you want is *charToChange = 'b';. The pointer charToChangeis a local variable (parameter) in setChar, but you can change what it points to using the prefix *operator and an assignment. Note that *charToChangeis a character, too, not a string.
你想要的是*charToChange = 'b';. 指针charToChange是 中的局部变量(参数)setChar,但您可以使用前缀*运算符和赋值来更改它指向的内容。请注意,它*charToChange也是一个字符,而不是一个字符串。
回答by Frédéric Hamidi
You have to dereferencethe pointer passed to setChar()in order to modify the value it points to, not the pointer argument itself.
您必须取消引用传递给的指针setChar()才能修改它指向的值,而不是指针参数本身。
You also have to use the character literal 'B'instead of the string literal "B"(which is a pointer to char, not a char).
您还必须使用字符文字'B'而不是字符串文字"B"(它是指向 的指针char,而不是 a char)。
void setChar(char* charToChange)
{
*charToChange = 'B';
}
回答by Sahil Doshi
when you call setChar function with &resultas parameter ,it passes Address of result where it is stored.
当您使用&result作为参数调用 setChar 函数时,它会传递结果的存储地址。
so it gets assigned to char pointer * charToChange
所以它被分配给字符指针* charToChange
Let say Address of resultis [0x00000010]-> 'A'
And Address of charToChangeis [0x00000100]-> 0x00000010
假设结果的地址是 [0x00000010]-> 'A'
charToChange 的地址是[0x00000100]-> 0x00000010
Now When you try to write charToChange = "B";
现在当你尝试写charToChange = "B";
It Creates New Memory where it stores "B"
Let Say [0x00001000]-> 'B' && [0x00001001]-> '\0'
它创建了存储“B”的新内存
假设[0x00001000]-> 'B' && [0x00001001]-> '\0'
So While doing Assignment it stores
所以在做作业时它会存储
[0x00000100]-> 0x00001000
[0x00000100]-> 0x00001000
But Yet The Address 0x00000010is Pointing to A
但是地址0x00000010指向A
So it is Wrong
所以这是错误的
You should replace charToChange = "B";to
您应该替换charToChange = "B"; 到
*charToChange = 'B';
*charToChange = 'B';
So that Value at 0x00000100Becomes 'B'
因此0x00000100 处的值变为“B”
Always Remember
总记得
* Means ValueAt And
& Means AddressOf
* 表示 ValueAt 和
& 表示地址的
回答by Dario
You want to change the value the pointer points to, not the pointer itself.
您想更改指针指向的值,而不是指针本身。
Thus you need to dereference the pointer with *pointer:
因此,您需要使用以下命令取消引用指针*pointer:
void setChar(char* charToChange) {
*charToChange = 'B';
}
If you don't, you just change the local value of charToChange.
如果不这样做,您只需更改charToChange.
回答by Dave
You need to dereference it, and the literal must be a char rather than a string, i.e. use apostrophes rather than double quotes:
您需要取消引用它,并且文字必须是字符而不是字符串,即使用撇号而不是双引号:
void setChar(char* charToChange)
{
*charToChange = 'B';
}
回答by Chris Reid
C copies pointers that are passed in to functions as parameters. So inside the function you are manipulating a copy of the pointer, not the pointer itself. When the function exits, the pointer if it was changed as in
C 复制作为参数传入函数的指针。因此,在函数内部,您正在操作指针的副本,而不是指针本身。当函数退出时,指针是否已更改为
while (p++) if (*p = 'void f(char* s)
{
/* code stuff */
...
s = NULL;
...
return;
}
') break; //example
will return to its previous value and the copy will be destroyed. The object of memory location the pointer points to may be changed and that is the whole idea behind pass by reference. A common mistake is trying to null a pointer inside of a function and then discovering it not being null upon return from the function. On some systems you get back the pointer pointing to garbage or bad memory locations that crash your program when you try to read from or write to the variable.
将返回其先前的值,并且副本将被销毁。指针指向的内存位置对象可能会改变,这就是通过引用传递背后的整个想法。一个常见的错误是试图将函数内部的指针置空,然后在从函数返回时发现它不是空的。在某些系统上,当您尝试读取或写入变量时,您会返回指向垃圾或坏内存位置的指针,这些位置会导致程序崩溃。
##代码##upon return from fnow s = ? (previous value , NULL, or GARBAGE ) This happens most often with variables passed to functions defined in separate modules that take values by reference, or across execution contexts (like threads or shared memory between processes).
从f现在返回s = ? (previous value , NULL, or GARBAGE ) 这种情况最常发生在传递给在单独模块中定义的函数的变量时,这些函数通过引用或跨执行上下文(如线程或进程之间的共享内存)获取值。

