English 中文(简体)
内含字串的字串为何不相同?
原标题:String literals that contain - why aren t they the same?
  • 时间:2012-05-24 17:22:12
  •  标签:
  • c++
  • string

所以我做了以下测试:

char* a = "test";
char* b = "test";
char* c = "test";

现在的问题是:

(1) 它是否保证 a sub ? I know I m 比较地址。这不是要比较字符串,而是要比较是否将相同的字符串字典存储在一个存储位置

2) 为什么没有 a supplyc ? 编译者是否应该看到它们指的是同一个字符串?

3) 是否在 c 结尾处附加了额外的 ,即使它已经包含一个?

我不想为此问三个不同的问题, 因为它们似乎有些关联,抱歉。

注意: 标签是正确的, 我对 C++ 感兴趣 。 (尽管请具体说明 C 的行为是否不同 )

最佳回答

它能保证有b吗?

否。但第2.14.5/12节允许:

是否所有字符串字典都不同( 即存储在非重叠对象中) 是执行定义。 试图修改字符串字典的效果未定义 。

正如你从最后一句中看到的那样,使用char%/code>而不是char const/code>来代替char const/code>,是一种麻烦的诱因(而您的编译者应该拒绝它; 确保您有启用的警告和选择的高一致性级别)。

为什么不是一个 supplyc? 编译者是否应该看到他们指的是同一个字符串?

否,它们不需要使用相同的字符组。 一个有五个元素, 另一个有六个元素。 一个执行程序可以将这两个元素存储在重叠的存储中, 但这不需要 。

是否在 c 结尾处附加了额外的 , 即使它已经包含 之一?

问题回答

1 -绝对不是。 如果编译者选择分享相同的静态字符串, 可能会 : b

- 2 因为他们指的不是同一个字符串

- 3是的。 - 3是的。

C++ 与 C++ 之间没有区别, 但 C++ 编译者应该拒绝对非默认字符* 的指定 。

1) 它能保证这个b吗?

并不是。 请注意, 您正在比较地址, 地址可能指向不同的地点。 多数聪明的编译者会折叠这个重复的字面常数, 这样指示者可以比较相等, 但同样没有标准保证 。

(二) 为什么没有 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

您试图比较指针, 它们指向不同的内存位置。 即使您比较了指针的内容, 它们仍然是不平等的( 参见下个问题 ) 。

3) 是否在c的末尾附加了额外的,即使它已经包含一个?

是的 确实有 Yes, there is.

首先要注意的是,这应该是Const char*, 因为它是字符串字典的缩写。

  1. Both create arrays initialized with t e s t folowed by a (length = 5). Comparing for equality will only tell you if they both start with the same pointer, not if they have the same contents (though logically, the two ideas follow each other).
  2. A isn t equal to C because the same rules apply, a = t e s t and b = t e s t
  3. Yes, the compiler always does it and you shouldn t expicitly do in if you re making a string like this. If you however crated an array and manually populated it, you need to ensure you add the .

请注意,对于我的# 3, Const char[ ] = "哈罗世界", 也会自动得到 在结尾, 我反悔手工填充阵列, 没有编译者完成它 。

这里的问题是,你把指针和文字等同的概念重新混为一谈。

当您说 a = b a = c < /code> = c < code > 时,您会问所涉指针指向的物理地址是否相同。 测试与指针的文字内容无关 。

要获得文本等效, 您应该使用 strcmp

如果您在做指针比较, 而不是 $ = b, b = = = c, c = a。 除非编译器足够聪明, 足以注意到您前两个字符串是相同的 。

如果你做了strcmp(str, str), 那么所有的字符串都会以匹配的方式回来 。

我不确定汇编者是否会为c增加一个无效终止,但我猜会的。

正如在其他答复中已经说过的几次,您是比较指针。然而,我要补充的是,strcmp(b,c) 应该是真实的,因为它在第一个时停止检查。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...