嵌入字符串 - 数组或指针? [英] Embedded Strings -- Arrays or pointers?

查看:91
本文介绍了嵌入字符串 - 数组或指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果您有以下编译时字符串:


"文件格式未知。


你用它来存储什么样的变量?目前我正在使用:


char const str [] ="文件格式未知。;


你们有些人使用:


const char * const str ="文件格式未知。;


- Tomás



When you have compile-time strings like:

"The file is of unknown format."

What kind of variables do you use to store it? At the moment I''m using:

char const str[] = "The file is of unknown format.";

Do some of you use:

const char* const str = "The file is of unknown format.";

-Tomás

推荐答案

*Tomás:
当您有编译时字符串时:

文件格式未知。

您使用什么样的变量来存储它?目前我正在使用:

char const str [] ="文件格式未知。;

你们有些人使用:

const char * const str ="文件格式未知。;
When you have compile-time strings like:

"The file is of unknown format."

What kind of variables do you use to store it? At the moment I''m using:

char const str[] = "The file is of unknown format.";

Do some of you use:

const char* const str = "The file is of unknown format.";




也许有人这样做,但我认为它会抛弃这些原始数据的b $ b信息,并写下更多信息来做到这一点。


还有一个问题是是否要写


std :: string const str ="文件格式未知;


,至少有一个学习我认为的语言

独家使用std :: string是一个好主意,而不是深入研究指针领域

和原始数组,只是没有得到_used_到低 - 级别的功能。


但是,有些情况下std :: string常量不是
可能或可取的。


当然,字符串可能会嵌入到某些资源中数据

结构在程序或动态库中,用于

国际化,但这取决于平台和工具依赖

和因此超出了这个小组的讨论范围。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和e中最烦人的事情是什么-mail?



Perhaps some do, but I think it would be less than smart to discard
information for such raw data, and to write more to do that.

There is also the question of whether to write

std::string const str = "The file is of unknown format";

and at least for one learning the language I think it''s a good idea to
use std::string exclusively, and not delve into the realm of pointers
and raw arrays, simply not get _used_ to the low-level features.

However, there are some scenarios where the std::string constant isn''t
possible or advisable.

And of course the strings may instead be embedded in some resource data
structure in the program or in a dynamic library, for
internationalization, but that is platform-dependent and tool-dependent
and therefore outside the scope of discussion in this group.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



$ b $bTomás写道:

Tomás wrote:
当你有编译时字符串时:

文件格式未知。

您使用什么样的变量来存储它?目前我正在使用:

char const str [] ="文件格式未知。;

你们有些人使用:

const char * const str ="文件格式未知。;

-Tomás
When you have compile-time strings like:

"The file is of unknown format."

What kind of variables do you use to store it? At the moment I''m using:

char const str[] = "The file is of unknown format.";

Do some of you use:

const char* const str = "The file is of unknown format.";

-Tomás




据我所知,他们之间没有差异。

我可能一无所知。完全一样。

char astr [] =" array hello" ;;

char * pstr =" pointer hello";


现在你有两个零区域和两个指针。


差异在文字中,如下所示:

char * pstr2 = {' 'e'',''r'',''r'',''o'',''r''}; //非法

char astr2 [] = pstr ;; / /非法

char astr3 [] = {''e'',''r'',''r'',''o'',''r'',''\ 0''}; //合法

char astr3 [] = {''e'',''r'',''r'',''o'',''r' '}; //合法,但不是字符串,因为没有

零附加


字符串由数组表示,它们不是数组。

/ Jesper



As far as I know there is no diffrence between them.
I might be ignorant as hell. Is exactly the same.
char astr[]="array hello";
char* pstr="pointer hello";

now you have two zeroterminated memory areas and two pointers.

The diffrence is in literals, as below:
char* pstr2={''e'',''r'',''r'',''o'',''r''};//illegal
char astr2[]=pstr;;//illegal
char astr3[]={''e'',''r'',''r'',''o'',''r'',''\0''};//legal
char astr3[]={''e'',''r'',''r'',''o'',''r''};//legal, but not a string since no
zero is appended

Strings are represented by arrays, they are not arrays.
/Jesper




< je **** @ alphacash.se>在消息中写道

新闻:11 ********************* @ g47g2000cwa.googlegro ups.com ...

$ b $bTomás写道:

<je****@alphacash.se> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...

Tomás wrote:
当您有编译时字符串时:

文件格式未知。

你用什么样的变量来存储它?目前我正在使用:

char const str [] ="文件格式未知。;

你们有些人使用:

const char * const str ="文件格式未知。;

-Tomás
When you have compile-time strings like:

"The file is of unknown format."

What kind of variables do you use to store it? At the moment I''m using:

char const str[] = "The file is of unknown format.";

Do some of you use:

const char* const str = "The file is of unknown format.";

-Tomás




据我所知,他们之间没有差异。

我可能一无所知。完全一样。

char astr [] =" array hello";

char * pstr =" pointer hello";

---

以上两个不一样。

pstr [1] =''a''; //非法

astr [1] =''a''; //法律


Sharad



As far as I know there is no diffrence between them.
I might be ignorant as hell. Is exactly the same.
char astr[]="array hello";
char* pstr="pointer hello";
---
The above two are not same.
pstr[1] = ''a''; // Illegal
astr[1] = ''a''; // Legal

Sharad


这篇关于嵌入字符串 - 数组或指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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