用 C 中的函数更改数组? [英] Changing an array with a function in C?

查看:29
本文介绍了用 C 中的函数更改数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用一个函数,我希望该函数将程序中字符串或数组的内容更改为常量.

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.

伪代码:

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;
}

我该怎么做?

推荐答案

您是通过值而不是引用传递指向数组的指针.应该是:

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;
}

您将hello"的地址提供给了 changeArray 函数,但是在函数中您更改了传递的值,而不是原始指针.我做的改变传递了指针的地址,在函数中改变了指针本身.

You gave the the address of "hello" to changeArray function, 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.

请不要char *blah = "hello"; 定义一个指向常量字符串的指针,以及*arr = "bingo";,这样都可以,但如果您考虑更改字符串本身,您将无法更改.

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.

当你将一个参数,甚至一个指针,传递给一个函数时,你实际上是将它复制到函数从那里读取它的某个地方(通常是堆栈).您不传递参数本身,而是传递它的副本.当函数修改它时(如在 arr = "bingo"; 中),它修改了变量的副本,而不是原始变量.所以为了改变变量本身,我们将变量的地址传递给函数 (changeArray(&blah); - & 表示 addressof-) 并且在函数中我们修改了存储在我们传递的地址中的变量 (*arr = "bingo"; - * 表示在地址arr).

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).

假设原始blah 指针位于地址0x00000000 中,包含"hello" 字符串的地址,例如0x00000010.如果将 blah 传递给函数,则将其复制到一个新变量 arr,例如位于地址 0x00000020 中

Assuming the original blah pointer is located in the address 0x00000000 and contains the address of "hello" string which is for example 0x00000010. if you pass blah to the function, you copy it to a new variable, arr, which is located in address 0x00000020 for example

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...)

现在如果你改变arr的内容,你会改变地址0x00000020中的值,而不是地址0x000000000中的值,所以blah仍然包含00000010.

now if you change the content of arr you change the value in address 0x00000020, but not the value in address 0x000000000, so blah still contains 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...)

相反,我们所做的是将 blah 的地址,即 0x00000000,复制到 arr 并在我们说的函数中 - 内容 of arr 是一个地址,转到这个地址并将其内容更改为指向bingo"字符串.所以现在地址 0x00000000(即 blah)中的内容指向bingo".

Instead what we do is copy the address of blah, which is 0x00000000, to arr and in the function we say - "the content of 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".

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...

这篇关于用 C 中的函数更改数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆