0

我想更改constant-character-array( const array[64]) 的内容。
下面是我的代码。
我的问题const char *append是,当作为常量字符指针( )传递给函数时,为什么常量字符数组不改变(不反映回来)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int function(char *d,const char *append)
{
 append = d; //changing the location of append.
 printf ("%s\n",append); //displays as sachintendulkar.
}

int main()
{
    char *d = NULL;
    const char append[]={'s','a','c','h','i','n'};
    d = calloc(sizeof(char),sizeof(append));
    strcpy(d,append);
    strcat(d,"tendulkar"); //appending
    function(d,append);
    printf ("%s\n",append); //Its displays as sachin instead of sachintendulkar???
}
4

2 回答 2

4

函数参数是按值传递的,当您为append内部的指针分配一个新值function()时,函数外部不会发生任何事情。

目前还不是很清楚你想要做什么......恒定数据的重点当然是你不应该改变它。

于 2012-05-28T09:51:05.240 回答
0

It is just a coincidence that the names of the parameters are the same as the variables in main. There is no connection between the names.

Your function works the same as if it was

int function(char *x, const char *y)
{
 y = x; //changing the location of y.
 printf ("%s\n", y); //displays as sachintendulkar.
}

You wouldn't expect that function to change the values inside main.

于 2012-05-28T15:24:22.697 回答