从早期的线程中神秘化的那种 [英] Sort of mystified from an earlier thread

查看:64
本文介绍了从早期的线程中神秘化的那种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这取自以下内容:

http://groups.google.com/group/comp....3e9afae83d061c


我引用:


嗯,这也适用于char **,因为字符串文字的类型为

char * in c。但总体思路仍然存在。


令我恼火的是,尽管如此,在没有C的情况下违反const的情况是_trivial_

求助于所有这些。


const char foo [] =" mystring" ;;

char * constviol = strchr(foo,* foo);


我不能得到的是'const char f [] =" mystring" ''定义

作为char,但原型定义如下:


char * strchr(const char * s,int c);


当foo被取消引用时(即* foo),为什么编译器不会b / b $ b $抱怨''int'之间的区别和''char''?


提前致谢。


Chad

This was taken from the following:

http://groups.google.com/group/comp....3e9afae83d061c

And I quote:

"Well, that''s also ok for char**, since string literals are of type
char * in c. The general idea still stands, though.

The thing that irritates me is that despite all this, it''s _trivial_
to violate const in C without resorting to all this.

const char foo[] = "mystring";
char *constviol = strchr(foo,*foo); "

What I don''t get is that that ''const char f[]="mystring" '' is defined
as a char, but the prototype is defined as the following:

char *strchr(const char *s, int c);

When foo gets de-referenced (ie *foo), how come the compiler doesn''t
complain about the difference between ''int'' and ''char''?

Thanks in advance.

Chad

推荐答案

2005年10月29日星期六21:51:46 -0400,Chad< cd ***** @ gmail.com>写道:
On Sat, 29 Oct 2005 21:51:46 -0400, Chad <cd*****@gmail.com> wrote:
"嗯,这也适用于char **,因为字符串文字在c中的类型为
char *。然而,总体思路仍然存在。

令我恼火的是,尽管如此,在不诉诸所有这一切的情况下违反const中的const也是_trivial_


const char foo [] =" mystring" ;;
char * constviol = strchr(foo,* foo);

我不能得到的是'const char f [] =" mystring" ''被定义为char,但原型定义如下:

char * strchr(const char * s,int c);

当foo被取消引用时(即* foo),为什么编译器不会抱怨''int''和''char''之间的区别?
"Well, that''s also ok for char**, since string literals are of type
char * in c. The general idea still stands, though.

The thing that irritates me is that despite all this, it''s _trivial_
to violate const in C without resorting to all this.

const char foo[] = "mystring";
char *constviol = strchr(foo,*foo); "

What I don''t get is that that ''const char f[]="mystring" '' is defined
as a char, but the prototype is defined as the following:

char *strchr(const char *s, int c);

When foo gets de-referenced (ie *foo), how come the compiler doesn''t
complain about the difference between ''int'' and ''char''?




我实际上也在想这个。我知道char是一个

整数类型,但我仍然会认为int和char会带来某种警告或者什么不是。我不确定我的理解

这一切都是怎么回事。


- 北极


-

使用Opera革命性的电子邮件客户端: http://www.opera.com/mail/


Arctic Fidelity写道:
Arctic Fidelity wrote:
周六,29日2005年10月21:51:46 -0400,Chad< cd ***** @ gmail.com>写道:
On Sat, 29 Oct 2005 21:51:46 -0400, Chad <cd*****@gmail.com> wrote:
"嗯,这也适用于char **,因为字符串文字在c中的类型为
char *。然而,总体思路仍然存在。

令我恼火的是,尽管如此,在不诉诸所有这一切的情况下违反const中的const也是_trivial_


const char foo [] =" mystring" ;;
char * constviol = strchr(foo,* foo);

我不能得到的是'const char f [] =" mystring" ''被定义为char,但原型定义如下:

char * strchr(const char * s,int c);

当foo被取消引用时(即* foo),为什么编译器不会抱怨''int''和''char''之间的区别?
"Well, that''s also ok for char**, since string literals are of type
char * in c. The general idea still stands, though.

The thing that irritates me is that despite all this, it''s _trivial_
to violate const in C without resorting to all this.

const char foo[] = "mystring";
char *constviol = strchr(foo,*foo); "

What I don''t get is that that ''const char f[]="mystring" '' is defined
as a char, but the prototype is defined as the following:

char *strchr(const char *s, int c);

When foo gets de-referenced (ie *foo), how come the compiler doesn''t
complain about the difference between ''int'' and ''char''?


<我实际上也在想这个。我知道char是一个
整数类型,但我仍然会认为int和char会带来某种警告或什么不是。我不知道我是怎么理解
这一切是怎么回事。

- 北极

-
使用Opera'革命性的电子邮件客户端: http://www.opera.com/mail/




除了草率的措辞之外,这是我最好的猜测是什么。

on。


当我们去* foo时,我们从字符串中获取每个字符。

在每次传递的内部,我们将有一个存储''m'的变量,tnen

''y''等等。如果我们做了类似的事情,那将是一样的。

char internal_string =''m'';


然后char将自动转换为整数(在strchr

int c参数上)。这可能解释了即使我启用了强制警告标志,gnu编译器也没有抱怨
抱怨。



Outside of the sloppy wording, here is my best guess on what is going
on.

When we go *foo, we are getting each character from the string.
Internally at each pass, we would have a varibale storing ''m'', tnen
''y'', etc. This would be the same has we done something like

char internal_string = ''m'';

Then char would be automatically converted to integer (on the strchr
int c parameter). This might explain wny the gnu compiler didn''t
complain even when I hard warning flags enabled.


Chad写道:

这取自以下内容:

http://groups.google.com/group/comp....3e9afae83d061c

我引用:

嗯,这也适用于char **,因为字符串文字在c中的类型为
char *。然而,总体思路仍然存在。

令我恼火的是,尽管如此,在不诉诸所有这一切的情况下违反const中的const也是_trivial_


const char foo [] =" mystring" ;;
char * constviol = strchr(foo,* foo);

我不能得到的是'const char f [] =" mystring" ''被定义为char,但原型定义如下:

char * strchr(const char * s,int c);

当foo被取消引用时(即* foo),为什么编译器不会抱怨''int''和''char''之间的区别?

This was taken from the following:

http://groups.google.com/group/comp....3e9afae83d061c

And I quote:

"Well, that''s also ok for char**, since string literals are of type
char * in c. The general idea still stands, though.

The thing that irritates me is that despite all this, it''s _trivial_
to violate const in C without resorting to all this.

const char foo[] = "mystring";
char *constviol = strchr(foo,*foo); "

What I don''t get is that that ''const char f[]="mystring" '' is defined
as a char, but the prototype is defined as the following:

char *strchr(const char *s, int c);

When foo gets de-referenced (ie *foo), how come the compiler doesn''t
complain about the difference between ''int'' and ''char''?




因为将char转换为int没有问题,

除非(CHAR_MAX> INT_MAX)

看起来似乎没有在任何托管系统中都是如此。


-

pete



Because there''s no problem converting a char to an int,
unless (CHAR_MAX > INT_MAX)
which doesn''t seem to be the case in any hosted sysytems.

--
pete


这篇关于从早期的线程中神秘化的那种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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