我想更改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???
}
答案 0 :(得分:4)
函数参数按值传递,当您为append
内的指针function()
分配新值时,在函数外部没有任何反应。
目前还不是很清楚你要做什么......当然,持续数据的关键在于你不应该改变它。
答案 1 :(得分:0)
参数的名称与main中的变量相同只是巧合。名字之间没有联系。
您的功能与
的功能相同int function(char *x, const char *y)
{
y = x; //changing the location of y.
printf ("%s\n", y); //displays as sachintendulkar.
}
您不希望该函数更改main中的值。