char []和char *作为参数有什么不同 [英] what's the different between char[] and char* as parameters
问题描述
如果我们在类似的函数参数中使用char []
void st(char x [])
{cout<< x;}
和char *作为同一函数中的参数而不是char x []
会有什么不同
在我的实验中我得出的结论是
with char []我可以更改指定的值,而不像char *
但是另一个问题
为什么分配给char []的值当我在函数中更改它时
它也会改变传递的变量
ie:
void st(char x [])
{
x [0] =' 'e'';
cout<< x;
}
int main()
{
char q [] =" ; CPP"
st(q);
cout<< q;
}
在st(q)中它将返回qPP那个合乎逻辑
但是
第二行会返回相同的结果
如何
i传递变量而不是引用,函数也不接受
引用
为什么第二行结果中的值改变了
if we use char[] in a function parameter like that
void st(char x[])
{cout << x;}
and char* as a parameter in the same function instead of char x[]
what would be different
in my experiments i conclude that
with char[] i can change the assigned value unlike char*
but in another question
why the value assigned to char[] when i change it in the function
it will also change the passed variable
ie:
void st(char x[])
{
x[0]= ''e'';
cout << x;
}
int main()
{
char q[]= "CPP"
st(q);
cout << q;
}
in st(q) it will return "qPP" that logical
but
the second line will return the same result
HOW
i pass a variable not a reference and the function also don''t accept
references
why the value changed in the result of the second line
推荐答案
Virtual_X写道:
Virtual_X wrote:
如果我们使用char []一个像这样的函数参数
void st(char x [])
{cout<< x;}
和char *作为同一函数中的参数而不是char x []
if we use char[] in a function parameter like that
void st(char x[])
{cout << x;}
and char* as a parameter in the same function instead of char x[]
没什么,他们有两种方式可以说同样的事情。
Nothing, they are two ways of saying the same thing.
会有什么不同
在我的实验中我得出的结论是
with char []我可以改变指定的值,不像char *
what would be different
in my experiments i conclude that
with char[] i can change the assigned value unlike char*
我不知道你有什么想法,但它''不真实。
I don''t know where you got that idea, but it''s untrue.
但是在另一个问题中
当我在函数中更改它时为什么分配给char []的值
它也会改变传递的变量
but in another question
why the value assigned to char[] when i change it in the function
it will also change the passed variable
因为它是指针。
你的教科书应该解释所有这些。
Brian
Because it''s a pointer.
Your textbook should explain all of this.
Brian
Virtual_X< C。******* @ gmail .comwrote:
Virtual_X <C.*******@gmail.comwrote:
如果我们在类似的函数参数中使用char []
v oid st(char x [])
{cout<< x;}
和char *作为同一函数中的参数而不是char x []
会有什么不同
if we use char[] in a function parameter like that
void st(char x[])
{cout << x;}
and char* as a parameter in the same function instead of char x[]
what would be different
没什么。当用作函数参数时,数组将衰减为指向第一个元素的
指针。你不能通过值将数组传递给
函数。
Nothing. When used as a function parameter, arrays will decay into
pointers to the first element. You cannot pass an array by value to a
function.
在我的实验中我的结论是
with char []我可以更改赋值,而不像char *
in my experiments i conclude that
with char[] i can change the assigned value unlike char*
你是什么意思?在这两种情况下,您都会将指针传递给数组的第一个元素
。你不能改变传递给函数的值(
指针),但你可以改变它指向的值。
What do you mean? In both cases you will be passing a pointer to the
first element of the array. You cannot change the value (of the
pointer) passed to the function, but you can change what it points to.
但是在另一个问题
为什么分配给char []的值当我在函数中更改它时
它也会改变传递的变量
ie:
void st(char x [])
{
x [0] =' 'e'';
cout<< x;
}
int main()
{
char q [] =" ; CPP"
st(q);
cout<< q;
}
在st(q)中它将返回qPP逻辑
but in another question
why the value assigned to char[] when i change it in the function
it will also change the passed variable
ie:
void st(char x[])
{
x[0]= ''e'';
cout << x;
}
int main()
{
char q[]= "CPP"
st(q);
cout << q;
}
in st(q) it will return "qPP" that logical
不,它应该将q更改为ePP。
No, it should change q to be "ePP".
但是
第二行将返回相同的结果
怎么回事
i传递一个变量而不是引用而且函数也没有''不接受
引用
为什么第二行的结果改变了价值
but
the second line will return the same result
HOW
i pass a variable not a reference and the function also don''t accept
references
why the value changed in the result of the second line
因为你传递了一个指针。见上文。
对于这些基本问题,也许你最好发布在
alt.comp.lang.learn.c-c ++
-
Marcus Kwok
用''net''替换''invalid''回复
Because you passed a pointer. See above.
For these basic questions, maybe you would be better posting in
alt.comp.lang.learn.c-c++
--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply
但
第二行将返回相同的结果
the second line will return the same result
如何
HOW
i传递一个变量而不是引用而且函数也不接受
引用
为什么第二行结果中的值发生了变化
i pass a variable not a reference and the function also don''t accept
references
why the value changed in the result of the second line
因为你传了一个指针。往上看。
Because you passed a pointer. See above.
如何将指针传递给char []
how i pass a pointer to char[]
这篇关于char []和char *作为参数有什么不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!