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

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

问题描述

我要调用一个函数,我想该函数在程序中的字符串或数组的内容更改为一个常数。

的伪code:

  some_array =你好
打印some_array #prints你好
changeArray(some_array)
打印some_array #prints宾果

我知道我要的指针传递给这个函数。下面是我写的,

 无效changeArray(字符* ARR){
    ARR =宾果;
}INT主(INT ARGC,为const char * argv的[]){
    字符*等等=你好;
    的printf(数组为%s \\ n,等等);
    changeArray(等等);
    的printf(数组为%s \\ n,等等);
    返回EXIT_SUCCESS;
}

我怎样才能做到这一点?


解决方案

您是按值而不是按引用传递的指针数组。它应该是:

 无效changeArray(字符** ARR){
    * ARR =宾果;
}INT主(INT ARGC,为const char * argv的[]){
    字符*等等=你好;
    的printf(数组为%s \\ n,等等);
    changeArray(安培;等等);
    的printf(数组为%s \\ n,等等);
    返回EXIT_SUCCESS;
}

您给了你好的地址 changeArray 的功能,但在功能上更改了值传递,而不是原来的指针。我提出的变化传递指针的地址,和指针本身在功能被改变。

请不的char *等等=你好; 定义一个指针常量字符串,以及 * ARR =宾果; ,这既是好的,但如果你考虑改变字符串本身,你就不能。

编辑:

当您传递参数,甚至一个指针,一个函数,你实际上它复制到一些地方的功能进行阅读(通常是堆栈)。你不传递参数本身,而是它的一个副本。当函数修改它(如在 ARR =宾果; ),它修改变量,而不是原始变量的副本。因此,为了改变变量的值,我们的变量的地址传递给函数( changeArray(安培;等等); - 在&安培; 办法的地址 - ),并在功能上,我们修改存储在我们传递的地址变量( * ARR =宾果; - 在 * 表示在地址变量改编

假设原始的等等指针位于地址00000000,并包含你好字符串的地址其中例如是0x00000010。如果你通过等等的功能,您将它复制到一个新的变量,改编,它位于地址0x00000020例如:

 变量地址内容
-------------------------------
嗒嗒00000000 00000010(点你好)
你好00000010你好(这只是一个例子,所以不要苦了我:))
ARR 00000020 00000010
宾果00000030宾果(并再次...)

现在,如果你改变的内容改编您在地址0x00000020改变数值,但不是在地址0x000000000价值,所以等等仍然包含00000010。

 变量地址内容
-------------------------------
嗒嗒00000000 00000010(点你好)
你好00000010你好(这只是一个例子,所以不要苦了我:))
ARR 00000020 00000030(点为宾果)
宾果00000030宾果(并再次...)

相反,我们要做的就是复制等等的地址,该地址为0x00000000,为改编键,在功能我们说 - 改编的内容地址后,去这个地址并更改其内容指向宾果串。所以现在在0x00000000地址的内容(这是等等)指向宾果。

 变量地址内容
-------------------------------
嗒嗒00000000 00000030(点为宾果)
你好00000010你好(这只是一个例子,所以不要苦了我:))
ARR 00000020 00000000(点`blah`)
宾果00000030宾果(并再次...)

希望我没有混淆你...

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?

解决方案

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

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.

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

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

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

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天全站免登陆