C语言 在 C 中通过引用正确地将字符数组和字符指针传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15606919/
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
Correctly pass a char array and char pointer to function by reference in C
提问by AisIceEyes
Is there a right way to call a char array and a char pointer to go to a function but it's pass by reference where it will also be manipulated?
有没有正确的方法来调用一个字符数组和一个字符指针来转到一个函数,但它是通过引用传递的,它也将被操纵?
Something like this:
像这样的东西:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void manipulateStrings(char *string1, char *string2[])
{
strcpy (string1, "Apple");
strcpy (string2, "Banana");
printf ("2 string1: %s", string1);
printf ("2 string2: %s", &string2);
}
int main ()
{
char *stringA;
char stringB[1024];
stringA = (char *) malloc ( 1024 + 1 );
strcpy (stringA, "Alpha");
strcpy (stringB, "Bravo");
printf ("1 stringA: %s", stringA);
printf ("1 stringB: %s", stringB);
manipulateStrings(stringA, stringB);
printf ("3 stringA: %s", stringA);
printf ("3 stringB: %s", stringB);
return 0;
}
I am not sure if I'm understanding correctly how to pass such variables to a function and change the values of those variables who happen to be char / strings
我不确定我是否正确理解如何将这些变量传递给函数并更改那些碰巧是 char/strings 的变量的值
Edit: My question is - How would you be able to change the values of the two strings in the function?
编辑:我的问题是 - 您将如何更改函数中两个字符串的值?
回答by Ed S.
There is no such thing as pass by reference in C. Everything in C is passed by value. This leads to the solution you need; add another level of indirection.
C 中没有通过引用传递这样的东西。C 中的所有内容都是通过值传递的。这将导致您需要的解决方案;添加另一个间接级别。
However, your code has other problems. You don't need to pass a pointer to pointer (or pointer to array) because you are not mutating the input, only what it refers to. You want to copy a string. Great. All you need for that is a pointer to char initialized to point to a sufficient amount of memory.
但是,您的代码还有其他问题。您不需要传递指向指针(或指向数组的指针)的指针,因为您不会改变input,只改变它所指的内容。你想复制一个字符串。伟大的。您所需要的只是一个指向 char 的指针,该指针被初始化为指向足够的内存量。
In the future, if you need to mutate the input (i.e., assign a newvalue to it), then use a pointer to pointer.
将来,如果您需要对输入进行变异(即为其分配新值),则使用指向指针的指针。
int mutate(char **input)
{
assert(input);
*input = malloc(some_size);
}
int main(void)
{
/* p is an uninitialized pointer */
char *p;
mutate(&p);
/* p now points to a valid chunk of memory */
free(p);
return 0;
}
回答by SevenBits
You are currently doing:
您目前正在做:
manipulateStrings(stringA, stringB);
manipulateStrings(stringA, stringB);
And printing from within manipulateStrings. Instead, to verify you're doing it correctly, I would do:
并从内部打印manipulateStrings。相反,为了验证您是否正确执行,我会这样做:
manipulateStrings(stringA, stringB);
printf ("2 string1: %s", string1);
printf ("2 string2: %s", &string2);
And not print from within that function. That way, you can test if the variables are being copied by-value or by-reference.
而不是从该功能内打印。这样,您可以测试变量是按值复制还是按引用复制。

