简体   繁体   English

将字符串转换为 integer 时,编译器(C 语言)会做什么?

[英]What does the compiler (in C) do when converting a string into an integer?

Let's start with this example:让我们从这个例子开始:

#include <stdio.h>

int main()
{
    int x = "string";
    printf("%d", x);
}

Ouput -> "12221232"输出->“12221232”

I know this code example is syntactically correct semantically false.我知道这个代码示例在语法上是正确的,语义上是错误的。 I just don't know what happens here exactly.我只是不知道这里到底发生了什么。 The text gets converted into an integer, but how?文本被转换为 integer,但是如何转换? Can someone please explain?有人可以解释一下吗?

The value you observe is an address where "string" is stored.您观察到的值是存储"string"的地址。 The literal "string" is actually cast to a pointer of type const char * that points to the actual data.文字"string"实际上被转换为指向实际数据的const char *类型的指针。 This pointer (address of "string" data) is next cast to int which you observe as output of printf() .该指针( "string"数据的地址)接下来将转换为int ,您可以将其视为printf()的 output 。

In int x = "string";int x = "string"; , two things of note happen: ,发生了两件事:

  • The array represented by "string" is automatically converted to a pointer to its first element. "string"表示的数组会自动转换为指向其第一个元素的指针。 So this is some address in memory, of type char * .所以这是 memory 中的某个地址,类型为char *
  • The address is converted to an int .地址被转换为int

This conversion is specified by C 2018 6.3.2.3 6, which says:此转换由 C 2018 6.3.2.3 6 指定,其中表示:

Any pointer type may be converted to an integer type.任何指针类型都可以转换为 integer 类型。 Except as previously specified, the result is implementation-defined.除非前面指定,结果是实现定义的。 If the result cannot be represented in the integer type, the behavior is undefined…如果结果不能以 integer 类型表示,则行为未定义……

Footnote 69 tells us the implementation-defined conversion is supposed to be “consistent” with the addressing structure of the environment the program runs in. Most modern systems use a “flat” address space, so the result of converting an address to an int will be the address as you may know it from seeing addresses in the debugger.脚注 69 告诉我们,实现定义的转换应该与程序运行环境的寻址结构“一致”。大多数现代系统使用“平面”地址空间,因此将地址转换为int的结果将是您在调试器中查看地址时可能知道的地址。 But that is provided the address fits in an int .但前提是地址适合int

If your system uses eight-byte addresses and four-byte int , then many addresses will not fit in an int .如果您的系统使用八字节地址和四字节int ,那么许多地址将不适合int In this case, the behavior is not defined.在这种情况下,行为未定义。 A common behavior is for C implementation to use just four bytes of the address to make the int . C 实现的常见行为是仅使用地址的四个字节来生成int Alternatively, it might set the int to all zeroes or all ones, or it might leave “garbage” in the int , or it might trap.或者,它可能会将int设置为全零或全一,或者它可能会在int中留下“垃圾”,或者它可能会陷入陷阱。

It's not converting the text to an integer, it is printing the value memory location of the literal string .它没有将文本转换为 integer,它正在打印值 memory location of the literal string Try this:尝试这个:

#include <stdio.h>

int main()
{
    int x = "string";
    printf("x = %d\n", x);
    printf("string = %d\n", "string");
}

If you run this you'll see that x and the literal "string" have the same memory address.如果你运行它,你会看到x和文字"string"具有相同的 memory 地址。

Sample output样品 output

x = 4202512
string = 4202512

Although as @pmg commented, this is not necessarily the case.尽管正如@pmg评论的那样,情况不一定如此。

In summary, what you are seeing is that the address of the string "string" is being assigned to x , which is where x is getting this value.总之,您所看到的是字符串"string"的地址被分配给x ,这是x获取此值的位置。

int x = "string"; is a constraint violation of simple assignment, see "Pointer from integer/integer from pointer without a cast" issues .是违反简单赋值的约束,请参阅“来自整数的指针/来自没有强制转换的指针的整数”问题

So the code is not valid C, has never been valid C and will not compile cleanly, see What must a C compiler do when it finds an error?所以代码是无效的 C,从来不是有效的 C 并且不会编译干净,请参阅C 编译器在发现错误时必须做什么? . . Speculating about why an invalid, non-standard C program prints something isn't very meaningful.推测为什么一个无效的、非标准的 C 程序会打印一些东西并不是很有意义。

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

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