有些疑惑! [英] a few doubts!

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

问题描述

#include< stdio.h>

#include< conio.h>

#include< string.h>

main( )

{

char * a =" bcd";

clrscr();

strcpy(a ,你好);

a =" fgh";

a [0] =''t'';

printf(" ;%s",a);

}

现在,在TC中绝对没有错误.....我认为它是

应该........当我宣布a作为char *并将其分配给某些

字符串然后它应该是一个常量并且不能做像[0] =这样的事情''4''

和东西........事实上整个事情在这里工作正常....所以为什么

它应该正常工作?

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char * a= "bcd";
clrscr();
strcpy(a,"hello");
a = "fgh";
a[0] = ''t'';
printf("%s",a);
}
now, in TC there is absolutely no error .....i thought it
should........coz'' when i declare a as a char * and assign it to some
string then it should be a constant and cannot do things like a[0] = ''4''
and stuff........infact the entire thing here works properly....so why
should it work properly??

推荐答案

-----开始PGP签名消息-----

哈希:SHA1


maadhuu写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

maadhuu wrote:
#include< stdio.h>
#include< conio.h>


conio.h不是标准标题。它的内容可能是任何东西,并且可能会以我们无法确定的方式影响您的程序


#include< string.h>
main()


main()接受两个参数或者没有,并返回一个int

你应该使用这两个合法变体之一,可能

int main(无效)

{
char * a =" bcd" ;;


a被声明为指向4个字符数组的指针。

a可以更改

4个字符的数组不能是改变了

clrscr();


clrscr()不是标准函数。它的动作和副作用可以做任何事情,并且会以我们无法确定的方式影响你的程序。

strcpy(a,hello);


至少,未定义的行为,因为* a只有4个字符长,而strcpy()的

副作用(和函数)将是使用

6 char长值覆盖该空间。 6个字符不适合4个字符的空间。


这里知识渊博的人会告诉你这是否是非法的行为。 (因为strcpy()的副作用,这将是尝试

来覆盖char常量)或只是未定义的行为。


a =fgh;


法律。你没有替换常量char数组,你正在替换

变量指针。

a [0] =''t'';


IIUC,这应该是非法的。

printf("%s",a);


main()返回一个int。在这里返回一个。

(除非你的编译器符合C99,在这种情况下,返回值是

可选,默认为预定值)。 />
}

现在,在TC中绝对没有错误.....我认为它应该........当时我应该......将a声明为char *并将其分配给某些
字符串然后它应该是一个常量,并且不能执行像[0] ='''4''
和东西....... 。实际上这里的整个工作都正常....所以为什么
它应该正常工作?
#include<stdio.h>
#include<conio.h>
conio.h is not a standard header. It''s contents could be anything, and could
affect your program in ways we cannot determine

#include<string.h>
main()
main() takes either two arguments or none, and returns an int
you should use one of the two legal variants here, likely
int main(void)
{
char * a= "bcd";
a is declared as a pointer to an array of 4 char.
a can be altered
the array of 4 char cannot be altered
clrscr();
clrscr() is not a standard function. It''s actions and side-effects could do
anything, and will affect your program in ways we cannot determine.
strcpy(a,"hello");
At least, undefined behaviour, because *a is only 4 char long, and the
side-effect (and function) of strcpy() would be to overwrite that space with a
6 char long value. 6 chars don''t fit into a 4 char space.

The more knowledgable ones around here will tell you whether this is "illegal
behaviour" (because of the side-effect of strcpy(), which would be to attempt
to overwrite a char constant) or just "undefined behaviour".

a = "fgh";
Legal. You are not replacing the constant char array, you are replacing the
variable pointer.
a[0] = ''t'';
IIUC, this should be illegal.
printf("%s",a);
main() returns an int. return one here.
(Unless, your compiler is C99 compliant, in which case, the return value is
optional and defaults to a pre-determined value).
}
now, in TC there is absolutely no error .....i thought it
should........coz'' when i declare a as a char * and assign it to some
string then it should be a constant and cannot do things like a[0] = ''4''
and stuff........infact the entire thing here works properly....so why
should it work properly??



编译器是不合规的?

编译器坏了?

- -

Lew Pitcher


Master Code Code& JOAT-in-training |可根据要求提供GPG公钥

注册Linux用户#112576( http:/ /counter.li.org/

Slackware - 因为我知道我在做什么。

----- BEGIN PGP SIGNATURE-- ---

版本:GnuPG v1.2.4(GNU / Linux)

iD8DBQFCc5PDagVFX4UWr64RAs8TAJ4p + ZyxVWzyLHSghtGc9y AdO9altQCgkxAZ

TDeV / fVJ3HZ6ZPh / iGDjvb8 =

= jTH7

----- END PGP SIGNATURE -----


The compiler is non-compliant?
The compiler is broken?
- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I''m doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCc5PDagVFX4UWr64RAs8TAJ4p+ZyxVWzyLHSghtGc9y AdO9altQCgkxAZ
TDeV/fVJ3HZ6ZPh/iGDjvb8=
=jTH7
-----END PGP SIGNATURE-----


" maadhuu"写道:
"maadhuu" writes:
#include< stdio.h>
#include< conio.h>
#include< string.h>
main()
{
char * a =" bcd";
clrscr();
strcpy(a," hello");
a =" fgh" ;
a [0] =''t'';
printf("%s",a);
}

现在,在TC中有绝对没有错误.....我认为它应该........因为当我宣布一个char *并将其分配给一些
字符串然后它应该是一个常量,不能做像[0] =''4''
和东西........事实上整个事情在这里正常工作....所以为什么
它应该工作正确的??
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char * a= "bcd";
clrscr();
strcpy(a,"hello");
a = "fgh";
a[0] = ''t'';
printf("%s",a);
}
now, in TC there is absolutely no error .....i thought it
should........coz'' when i declare a as a char * and assign it to some
string then it should be a constant and cannot do things like a[0] = ''4''
and stuff........infact the entire thing here works properly....so why
should it work properly??




如果这不起作用肯定会更好,但我并不感到惊讶

它确实如此。只有语言律师可以确定你的编译器是否违反了某些规定的规则,而不是简单地让一些草率的代码通过。


strcpy工作的事实特别令人不安。我认为对抗Schildt及其书籍的部分毒品是他实际上曾经故意写过类似代码的b
。可能不是太大的方面strcpy,

虽然。


FWIW它在DevC(mingw)上失败了,我希望它在大多数情况下失败,如果没有

all,现代编译器。 。



It would certainly be nicer if this didn''t work, but I am not surprised that
it does. Only a language lawyer can determine whether your compiler breaks
some mandated rule, as opposed to simply letting some sloppy code through.

The fact that the strcpy works is especially troubling. I think part of the
venom against Schildt and his books is that he actually used to write
similar code on purpose. Probably not the strcpy with the too big aspect,
though.

FWIW it fails on DevC (mingw) and I would expect it to fail on most, if not
all, modern compilers. .


maadhuu写道:
maadhuu wrote:
#include< stdio.h>
#include< conio.h>
# include< string.h>
main()
{* * char * a =" bcd";
clrscr();
strcpy(a," hello" ;);
a =" fgh";
a [0] =''t'';
printf("%s",a);
}

现在,在TC中绝对没有错误.....我认为它应该........当我宣布a作为char *并且分配时...它对一些
字符串然后它应该是一个常量,不能做像[0] ='''4''
和东西........事实上整个事情在这里正常工作....那么为什么
它应该正常工作?
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char * a= "bcd";
clrscr();
strcpy(a,"hello");
a = "fgh";
a[0] = ''t'';
printf("%s",a);
}

now, in TC there is absolutely no error .....i thought it
should........coz'' when i declare a as a char * and assign it to some
string then it should be a constant and cannot do things like a[0] = ''4''
and stuff........infact the entire thing here works properly....so why
should it work properly??




它运作正常,因为它既不是一个重要的演示,有很大的

销售挂掉了它,也没有把它展示给你的老板。


标准说你不能写字符串文字,它

d如果你这样做,不要说它会产生错误或崩溃。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址是垃圾邮件,但它是真的,我读了它。



It worked properly because it was neither an important demo with a large
sale hanging off it nor you showing it to your boss.

The standard says you are not allowed to write to a string literal, it
does not say it will generate an error or crash if you do.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


这篇关于有些疑惑!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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