函数参数为char [] [英] Function argument as char []

查看:63
本文介绍了函数参数为char []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void foo(char str [])

{


char strA [] =" Hello";


strA [0] = strA [0];


str [0] = str [0];

}


void main()

{

foo(" abcd");

}

行:

str [0] = str [0];抛出异常


而strA [0] = strA [0];工作正常。


为什么?

void foo(char str[])
{

char strA[] = "Hello";

strA[0] = strA[0];

str[0] = str[0];
}

void main()
{
foo("abcd");
}
Line:
str[0] = str[0]; throws an exception

whereas strA[0] = strA[0]; works fine.

Why?

推荐答案



sh********@gmail.com 于08/21/06 12:55写道, :


sh********@gmail.com wrote On 08/21/06 12:55,:

void foo(char str [])

{


char strA [] =" Hello";


strA [0] = strA [0];


str [0] = str [0];

}


void main()

{

foo(" abcd");

}


行:

str [0] = str [0];抛出异常


而strA [0] = strA [0];工作良好。
void foo(char str[])
{

char strA[] = "Hello";

strA[0] = strA[0];

str[0] = str[0];
}

void main()
{
foo("abcd");
}
Line:
str[0] = str[0]; throws an exception

whereas strA[0] = strA[0]; works fine.



这是comp.lang.c中的问题1.32。经常

问题(FAQ)列表

http://www.c-faq.com/


-
Er*********@sun.com

This is Question 1.32 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/

--
Er*********@sun.com


文章< 11 ********************** @ m79g2000cwm.googlegroups .com>,

< sh ******** @ gmail.comwrote:
In article <11**********************@m79g2000cwm.googlegroups .com>,
<sh********@gmail.comwrote:

> void foo(char str [])
{
char strA [] =" Hello" ;;
strA [0] = strA [0];
str [0] = str [0];
}
>void foo(char str[])
{
char strA[] = "Hello";
strA[0] = strA[0];
str[0] = str[0];
}


> void main()
>void main()



void不是main()必须支持的返回类型之一,

虽然允许特定实现支持

main的附加声明( )。你的代码这次编译了;

在下一个编译器版本中它可能没有。

void is not one of the return types for main() which must be supported,
though it is allowed for a particular implementation to support
additional declarations for main() . Your code compiled this time;
in the next compiler release it might not.


> {

foo(" abcd");
}
>{
foo("abcd");
}


>行:
str [0] = str [0] ;抛出异常
而strA [0] = strA [0];工作正常。

为什么?
>Line:
str[0] = str[0]; throws an exception
whereas strA[0] = strA[0]; works fine.
Why?



这无疑是在C FAQ中。


char strA [] =" Hello" ;;

创建一个长度为6的字符数组,并使用

字符H,e,l,l, 'o''和''\'''。

这个数组是可写的,字符串

中给出的字符只是初始值 - 使用初始化程序中的char列表是

的简写。


foo(" abcd");

将包含

" abcd \0"的文字字符串的地址传递给foo。 。它是传递的指针,而不是单个

字符。 C实现被允许将这种类型的所有文字

字符串放入只读内存中。


如果你要改变你的foo()来使用


char * strA =" Hello";


那么你会发现它的相同异常行为,因为

在这种情况下,存储的是指向允许为只读的

文字字符串的指针。


作为快速摘要:如果一个给定的双引号字符串作为指向存储的指针,那么该字符串应该被假定为

是只读的;但是如果给定的双引号字符串在一个声明

中,以显示要标记为字符数组的字符,

那么结果字符数组是可写的。

-

如果你撒谎到编译器,它将报复。 - Henry Spencer

This is undoubtedly in the C FAQ.

char strA[] = "Hello";
creates a char array of length 6 and initiazes it with the
characters ''H'', ''e'', ''l'', ''l'', ''o'', and ''\0''.
This array is writable, with the characters given in the string
considered to be just initial values -- it is short-hand for
using a list of char in the initializer.

foo("abcd");
passes to foo the address of a literal string that contains
"abcd\0" . It is the pointer that is passed, not the individual
characters. C implementations are allowed to put all literal
strings of this type into read-only memory.

If you were to change your foo() to use

char *strA = "Hello";

then you would find the same exception behaviour for it, as
in that case too what is stored is the pointer to the
literal string that is allowed to be read-only.

As a quick summary: if a given double-quoted string is acting
as a pointer to storage, then the string should be assumed to
be read-only; but if the given double-quoted string is in a declaration
to show what characters to put into something marked as a character array,
then the resulting character array is writable.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer



sh **** **** @gmail.com 写道:

void foo(char str [])

{


char strA [] =" Hello";


strA [0] = strA [0];


str [0] = str [0];

}


void main()
void foo(char str[])
{

char strA[] = "Hello";

strA[0] = strA[0];

str[0] = str[0];
}

void main()



不是main的有效声明。尝试:

int main(void);

Not a valid declaration for main. Try:
int main (void);


{

foo(" abcd");
{
foo("abcd");



主要回报int:

返回0;

Main returns int:
return 0;


}


行:

str [0] = str [0];抛出异常
}
Line:
str[0] = str[0]; throws an exception



C中没有引发异常。

问题中的行会产生未定义的行为,请参阅常见问题解答
< http://c-faq.com/> ;,问题1.32。

There are no throwing of exceptions in C.
The line in questions produces undefined behavior, see the FAQ
<http://c-faq.com/>, question 1.32.


而strA [0] = strA [0];工作正常。


为什么?
whereas strA[0] = strA[0]; works fine.

Why?



Robert Gamble

Robert Gamble


这篇关于函数参数为char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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