char *和char数组之间的区别 [英] Difference between char* and char array

查看:231
本文介绍了char *和char数组之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我:


1.

void main()

{

char temp [] =" Hello";


temp [0] =''K'';


}

2.


void main()

{


char * temp =" Hello";


temp [0] =''K'';


}

第一个提供访问冲突,而第二个完成

成功。我想知道为什么第一个给出运行时错误?我想b / b
想知道内部会发生什么?编译器如何解释这个

两个片段?

Can anyone tell me :

1.
void main()
{

char temp[] = "Hello";

temp[0]=''K'';

}
2.

void main()
{

char* temp = "Hello";

temp[0]=''K'';

}

1st one gives an access violation whereas second one is done
successfully. I want to know why the fist one gives a runtime-error? I
want to know what happens internally? How the compiler interprets this
two snippets?

推荐答案

am ********* @ gmail.com 写道:
am*********@gmail.com wrote:
谁能告诉我:

1.
void main()
{char temp [] =" Hello";

temp [0] =''K'';

}
2.

void main()
{
char * temp =" Hello";

temp [0] =''K'';

}

第一个给出一个访问违规,而第二个成功完成。


实际上,两者都不正确。 main必须总是返回int。

我想知道为什么第一个给出运行时错误?


第一个不应该产生错误,但第二个可能会产生错误。

我想知道内部会发生什么?编译器如何解释这两个片段?
Can anyone tell me :

1.
void main()
{

char temp[] = "Hello";

temp[0]=''K'';

}
2.

void main()
{

char* temp = "Hello";

temp[0]=''K'';

}

1st one gives an access violation whereas second one is done
successfully.
Actually, both are incorrect. main must always return int.
I want to know why the fist one gives a runtime-error?
The first shouldn''t produce an error, but the second may.
I want to know what happens internally? How the compiler interprets this
two snippets?




在第一个中,在函数中本地创建了一个包含6个字符的数组,并且

字符串文字Hello的内容被复制到它。之后,

你覆盖该数组的第一个元素。

在第二个片段中,指向第一个字符文字本身的指针是

创建的。这个指针最好是一个const char *,因为字符串

文字是常量,不能修改。当您尝试写入

temp [0]时,您将访问只读内存。



In the first one, an array of 6 char is created locally in the function and
the contents of the string literal "Hello" is copied into it. Afterwards,
you overwrite the first element of that array.
In the second snippet, a pointer to the first character literal itself is
created. This pointer should better be a const char*, because string
literals are constant and must not be modified. When you try to write to
temp[0], you access read-only memory.


实际上,两者都不正确。 main必须总是返回int。
Actually, both are incorrect. main must always return int.
我知道当你从main返回一些东西(整数)时它会返回到progammers环境。我也知道你是对的主要总是应该返回int。但是,为什么会那样?什么是使用它?
I know when you return something (integer) from main it goes back to the progammers environment. I also know you are right main always should return int. But, why is it like that? whats the use of it?






< am ********* @ gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
<am*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
实际上,两者都是不正确的。 main必须总是返回int。
Actually, both are incorrect. main must always return int.


我知道当你从main返回一些东西(整数)时
会回到progammers环境。我也知道你
是正确的主要总是应该返回int。但是,为什么会这样呢?什么是使用它?

I know when you return something (integer) from main it
goes back to the progammers environment. I also know you
are right main always should return int. But, why is it
like that? whats the use of it?




因为''整数来自main回到程序

环境。''环境可能会无论喜欢什么,包括

什么都没有。因此对于程序员而言,它并不是真正的利息,而是对返回值做了什么。我们必须遵守规则才重要

..即。标准。


如果你不这样做,想象一下返回值的环境

在堆栈上。因此,无论是否需要,程序的调用者都需要删除返回的

值。如果你返回无效,你永远不会把b $放在堆栈上......但环境不知道,

对吗?因此,不是擦拭你的(应该是但不存在)返回

值,它将删除其他信息..你可以期待这个

以一种不方便用户的方式结束。


问候

-

jb


(rot13中的回复地址,先解读)



Because the ''integer from main goes back to the programs
environment.'' The environment may do whatever it likes, including
nothing. So for the programmers point of view, it''s not really of
interest, what will be done with the return value. It''s only important
that we must play by the rules .. ie. the Standard.

If you do not, imagine an environment where return values are passed
on the stack. So the caller of your program needs to remove the return
value, no matter if it needs it or not. If you return void, you never
put something on the stack .. but the environment does not know that,
right? So instead of wiping your (should-be-but-is-not-existant) return
value, it will remove other information .. and you can expect this to
end in a not user-friendly way.

regards
--
jb

(reply address in rot13, unscramble first)


这篇关于char *和char数组之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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