简体   繁体   English

如何取消引用字符串文字?

[英]How can a string literal be dereferenced?

The following code will compare the character S with the first element of string literal "Stack" ,以下代码将字符 S 与字符串文字"Stack"的第一个元素进行比较,

As per my understanding, only pointers can be dereferenced, but this string literal works like a pointer?根据我的理解,只能取消引用指针,但是这个字符串文字像指针一样工作吗?

#include <stdio.h>
int main() {
    if('S'==*"Stack")
        printf("equal");
}

“String literal” is a name for a thing in source code, like “keyword” is a name for key words like int or switch in source code. “String literal”是源代码中一个事物的名称,就像“keyword”是源代码中intswitch等关键字的名称。

When a string literal appears in source code, an array of characters is created and filled with the characters of the string literal, plus a terminating null character (C 2018 6.4.5 6).当源代码中出现字符串文字时,会创建一个字符数组,并用字符串文字的字符填充,加上终止字符 null (C 2018 6.4.5 6)。

When an array is used in an expression, other than as the operand of sizeof , the operand of unary & , or as a string literal used to initialize some other array, it is automatically converted to a pointer to its first element (C 2018 6.3.2.1 3).当数组在表达式中使用时,除了作为sizeof的操作数、一元&的操作数,或作为用于初始化其他数组的字符串文字,它会自动转换为指向其第一个元素的指针 (C 2018 6.3 .2.1 3).

Thus, when "Stack" appears in source code, it results in a pointer to “S”, so *"Stack" is a char with the value for “S”.因此,当"Stack"出现在源代码中时,它会导致指向“S”的指针,因此*"Stack"是一个值为“S”的char

Similarly, "Stack"[1] is a char with the value for “t”.同样, "Stack"[1]是一个值为“t”的char

In C, a string literal is an array , since strings in C are arrays of characters.在 C 中,字符串文字是一个数组,因为 C 中的字符串是 arrays 个字符。

In C, when an array appears in an expression, almost without exception, what you get (the "value" of the array for the purposes of evaluating that expression) is a pointer to the array's first element .在 C 中,当一个数组出现在一个表达式中时,几乎毫无例外地,您得到的(用于计算该表达式的数组“值”)是指向数组第一个元素的指针

So if you say所以如果你说

int a[10];
*a = 7;

, that does precisely the same thing that a[0] = 7 would do. ,这与a[0] = 7所做的完全相同。 And, having done that, you could test it by saying if(*a == 7) .而且,完成后,您可以通过说if(*a == 7)来测试它。

And the situation is 100% analogous to the example you posted.情况与您发布的示例 100% 相似。 The string literal "Stack" is an array, so when you use it in an expression, its effective value is a pointer to its first element.字符串文字"Stack"是一个数组,因此当您在表达式中使用它时,它的有效值是指向其第一个元素的指针。

Expanding things out to see the "hidden" internal steps, it works identically to what would have happened if you had written展开内容以查看“隐藏”的内部步骤,它的工作方式与您编写的情况相同

char stringliteral[] = "Stack";        /* the string literal is actually an array */
char *temporary_pointer;
temporary_pointer = &stringliteral[0]; /* we implicitly get a pointer to the array's first element */
if('S' == *temporary_pointer)
    ...

Addendum: Another demonstration of this fact, that a string literal is actually an array, crops up if you're writing code to convert a number to its string representation.附录:这个事实的另一个证明,即字符串文字实际上是一个数组,如果您正在编写代码将数字转换为其字符串表示形式,就会出现。 Any integer-to-string algorithm inevitably ends up computing digit values which it must convert to the corresponding characters.任何整数到字符串的算法都不可避免地以计算必须转换为相应字符的数字值告终。 If you're working in base 10 or less, you can convert digits to characters by simply adding an offset: dig + '0' .如果您使用的基数为 10 或更少,则只需添加一个偏移量即可将数字转换为字符: dig + '0' But another way, which works for eg hexadecimal digits as well, is "0123456789abcdef"[dig] .但另一种方法也适用于例如十六进制数字,是"0123456789abcdef"[dig]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM