C语言 用 C 中的函数更改数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5611832/
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
Changing an array with a function in C?
提问by Alex
I want to call a function and I want that function to change the contents of a string or array in the program to a constant.
我想调用一个函数,我希望该函数将程序中的字符串或数组的内容更改为常量。
Psuedocode:
伪代码:
some_array = "hello"
print some_array #prints "hello"
changeArray(some_array)
print some_array #prints "bingo"
I know I have to pass the pointer to that function. Here's what I wrote,
我知道我必须将指针传递给该函数。这是我写的,
void changeArray(char *arr){
arr = "bingo";
}
int main(int argc, const char* argv[]){
char *blah = "hello";
printf("Array is %s\n",blah);
changeArray(blah);
printf("Array is %s\n",blah);
return EXIT_SUCCESS;
}
How can I do this?
我怎样才能做到这一点?
回答by MByD
You are passing the pointer to the array by value instead of by reference. It should be:
您是按值而不是按引用将指针传递给数组。它应该是:
void changeArray(char **arr){
*arr = "bingo";
}
int main(int argc, const char* argv[]){
char *blah = "hello";
printf("Array is %s\n",blah);
changeArray(&blah);
printf("Array is %s\n",blah);
return EXIT_SUCCESS;
}
You gave the the address of "hello" to changeArrayfunction, but in the function you changed the value passed, not the original pointer. The change I made passes the address of the pointer, and the pointer itself is changed in the function.
您将“hello”的地址提供给changeArray函数,但在函数中您更改了传递的值,而不是原始指针。我所做的更改传递了指针的地址,而指针本身在函数中发生了更改。
Please not char *blah = "hello";defines a pointer to a constant string, as well as *arr = "bingo";, this is both fine, but if you consider to change the string itself, you will not be able to.
请不要char *blah = "hello";定义指向常量字符串的指针,以及*arr = "bingo";,这都很好,但是如果您考虑更改字符串本身,您将无法更改。
EDIT:
编辑:
When you pass an argument, even a pointer, to a function, you actually copies it to some place where the function read it from there (usually the stack). You don't pass the argument itself, but a copy of it. When the function modifies it (like in arr = "bingo";) it modifies the copy of the variable, not the original variable. So in order to change the variable itself, we pass the address of the variable to the function (changeArray(&blah);- the &means address of-) and in the function we modify the variable stored in the address we passed (*arr = "bingo";- the *means the variable in the address arr).
当你将一个参数,甚至是一个指针,传递给一个函数时,你实际上是将它复制到函数从那里读取它的某个地方(通常是堆栈)。您不传递参数本身,而是传递它的副本。当函数修改它时(如arr = "bingo";),它修改了变量的副本,而不是原始变量。因此,为了改变变量的值,我们通过变量函数的地址(changeArray(&blah);-的&手段address of-)和功能,我们修改存储在地址变量我们通过(*arr = "bingo";-的*手段在地址变量arr)。
Assuming the original blahpointer is located in the address 0x00000000 and contains the address of "hello"string which is for example 0x00000010. if you pass blahto the function, you copy it to a new variable, arr, which is located in address 0x00000020 for example
假设原始blah指针位于地址 0x00000000 并包含"hello"字符串的地址,例如 0x00000010。如果传递blah给函数,则将其复制到一个新变量 ,arr例如位于地址 0x00000020 中
Variable Address content
-------------------------------
blah 00000000 00000010 (points to hello)
"hello" 00000010 "hello" (this is just an example, so don't be hard on me :) )
arr 00000020 00000010
"bingo" 00000030 "bingo" (and again...)
now if you change the content of arryou change the value in address 0x00000020, but not the value in address 0x000000000, so blahstill contains 00000010.
现在,如果您更改内容,则arr更改地址 0x00000020 中的值,而不是地址 0x000000000 中的值,因此blah仍包含 00000010。
Variable Address content
-------------------------------
blah 00000000 00000010 (points to hello)
"hello" 00000010 "hello" (this is just an example, so don't be hard on me :) )
arr 00000020 00000030 (points to "bingo")
"bingo" 00000030 "bingo" (and again...)
Instead what we do is copy the address of blah, which is 0x00000000, to arrand in the function we say - "the contentof arr is an address, go to this address and change its content to point to "bingo" string". so now the content in address 0x00000000 (which is blah) is pointing to "bingo".
相反,我们所做的是将blah0x00000000的地址复制到arr我们说的函数中 - “ arr的内容是一个地址,转到该地址并将其内容更改为指向“宾果”字符串”。所以现在地址 0x00000000(即blah)中的内容指向“宾果游戏”。
Variable Address content
-------------------------------
blah 00000000 00000030 (points to "bingo")
"hello" 00000010 "hello" (this is just an example, so don't be hard on me :) )
arr 00000020 00000000 (points to `blah`)
"bingo" 00000030 "bingo" (and again...)
Hope I didn't confuse you...
希望我没有迷惑你...
回答by Ferruccio
There are no arrays in your code. If you are actually trying to modify what your blah character pointer points to, then you need to pass a pointer to a pointer into the function. However, if you want to do this using arrays then you need to do something like:
您的代码中没有数组。如果您实际上是在尝试修改您的 blah 字符指针指向的内容,那么您需要将一个指向指针的指针传递给函数。但是,如果您想使用数组执行此操作,则需要执行以下操作:
void changeArray(char arr[]) {
// or you can use char *arr, it's the same thing from
// the C compiler's point of view
strcpy(arr, "blah");
// alternatively, you could set each element. i.e. arr[0] = 'b';
}
int main (int argc, char** argv) {
char blah[100] = "hello"; // this is an array
printf("Array is %s\n", blah);
changeArray(blah); // array decays to pointer
printf("Array is %s\n", blah);
return EXIT_SUCCESS;
}
回答by Batman
You need to pass a pointer to array not the array itself.
您需要将指针传递给数组而不是数组本身。
Another thing: add a control condition to your function. think: what will happen if "bingo" will be bigger than strlen(some_array) ? it will give you an error because in C you must malloc it if you need the array size to be dynamic !
另一件事:向您的函数添加控制条件。想一想:如果“bingo”大于 strlen(some_array) 会发生什么?它会给你一个错误,因为在 C 中,如果你需要动态的数组大小,你必须 malloc 它!

