为什么是“Hello World” const char *? [英] Why is "Hello World" const char* ?

查看:91
本文介绍了为什么是“Hello World” const char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


为什么所有的C / C ++人都写道:


const char * str =" Hello" ;;





const char [] str =" Hello";


为什么这不是更优雅的方式:


const unsigned char * str =" Hello" ;; (或[])


??


我的Reasson:你见过ASCII码:-50?如果你使用''char''

而不是''unsigned char'',扩展的ASCII Set

(> 127)将用负数表示。


谢谢!


汉斯

解决方案

Hans写道05/02/05:

为什么所有的C / C ++人都写道:


没有C / C ++这样的东西。有C,有C ++。他们

是不同的语言(例如在C中,''A''是int和C ++,''A''是

char)

const char * str =" Hello";



const char [] str =" Hello" ;;


两者都是正确的,但有不同的语义(指针与数组)。

为什么这不是更优雅的方式:
const unsigned char * str =" Hello" ;; (或[])

??


有时会出现类型错误。

我的Reasson:你见过ASCII码:-50?如果使用''char''代替
''unsigned char'',则扩展的ASCII集(> 127)
将由负数表示。




''char'','unsigned''char和''signed char''是三种不同的

类型。字符串的类型('char''以0结尾的数组)是

....''char''。期间。


请注意,根据实施情况,可以签署char汽车或

未签名。使用< limits.h>中定义的常量得到当前范围

值。


-

Emmanuel

C-FAQ : http://www.eskimo.com/~scs /C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html


C是一个锋利的工具


>为什么这不是更优雅的方式:


const unsigned char * str =" Hello" ;; (或[])

??




这里的char是签名还是未签名并不重要。如果你做了一些

数学的东西,如char /

char a;

a = 127;

a ++;

if(a> 0)....

是否签名可能是个问题。但是当你只想处理

文本时,它没有任何区别。


Hans写道:

为什么这不是更优雅的方式:

const unsigned char * str =" Hello" ;; (或[])



int main()

{

const unsigned char * str =" Hello" ;;

}

错误信息全部说明:

C:\ c> g ++ temp.cpp -o temp.exe

temp.cpp:在函数`int main()''中:

temp.cpp:3:错误:从`const char *''无效转换为`const unsigned

char *''

temp.cpp:4:2:警告:文件末尾没有换行符


C:\c> ; cl temp.cpp

Microsoft(R)32位C / C ++优化编译器版本14.00.41013(适用于80x86)


temp.cpp

temp.cpp(3):错误C2440:''初始化'':无法转换''const

char [6]''

到''const unsigned char *''

指向的类型不相关;转换需要

reinterpret_cast,C-

样式演员或函数式演员

C:\ c>



-

Ioannis Vranos

http://www23.brinkster.com/noicys


Hello,

Why all C/C++ guys write:

const char* str = "Hello";

or

const char[] str = "Hello";

Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])

??

My Reasson: have you ever seen ASCII-Code: -50? The extended ASCII Set
(>127) would be represented by a negative number if you use ''char''
instead of ''unsigned char''.

Thanks!

Hans

解决方案

Hans wrote on 05/02/05 :

Why all C/C++ guys write:
There is no such thing like ''C/C++''. There is C and there is C++. They
are different languages (e.g. in C, ''A'' is an int and in C++, ''A'' is a
char)
const char* str = "Hello";

or

const char[] str = "Hello";
Both are correct, but have different semantic (pointer vs array).
Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])

??
Would be a type error sometimes.
My Reasson: have you ever seen ASCII-Code: -50? The extended ASCII Set (>127)
would be represented by a negative number if you use ''char'' instead of
''unsigned char''.



A ''char'', an ''unsigned'' char and a ''signed char'' are three different
types. The type for a string (an array of ''char'' terminated by a 0) is
.... ''char''. Period.

Note that depending on the implementation, a ''char'' car be signed or
unsigned. Use constants defined in <limits.h> to get the current range
values.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"


> Why is this not a more elegant way:


const unsigned char* str = "Hello"; (or [])

??



It is not important whether char is signed or unsigned here. If you do some
maths things with char like
char a;
a = 127;
a++;
if(a > 0)....
whether it is signed or not may be a problem. But when you just wanna handle
text, it makes no difference.


Hans wrote:

Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])


int main()
{
const unsigned char* str = "Hello";
}
The error messages say it all:
C:\c>g++ temp.cpp -o temp.exe
temp.cpp: In function `int main()'':
temp.cpp:3: error: invalid conversion from `const char*'' to `const unsigned
char*''
temp.cpp:4:2: warning: no newline at end of file

C:\c>cl temp.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.41013 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
temp.cpp(3) : error C2440: ''initializing'' : cannot convert from ''const
char [6]''
to ''const unsigned char *''
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-
style cast or function-style cast

C:\c>


--
Ioannis Vranos

http://www23.brinkster.com/noicys


这篇关于为什么是“Hello World” const char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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