简体   繁体   English

在C中将int转换为char时出现分段错误

[英]Segmentation fault when casting int to char in C

I have a very simple C program. 我有一个非常简单的C程序。 In main, I have this operation: 总的来说,我有这个操作:

int main(){
  int theNumber = 9009;
  printf("%s", (char *)theNumber);
}

And when I run it, it gives me a seg fault. 而当我运行它时,它给了我一个段错误。 Any idea why? 知道为什么吗? Am I doing this wrong? 我做错了吗?

Expected Output 预期产量

This code should convert theNumber to a string and then print it. 此代码应将theNumber转换为字符串,然后打印出来。 So the output would be: 因此输出为:

9009

Actual Output 实际产量

A segmentation fault. 分段错误。

This is trying to print whatever is found at the address 9009 . 这试图打印在地址9009找到的任何内容。 Seeing as the operating system is not giving your program access to this address (it is likely being used for something else entirely) the operating system raises a segmentation fault. 好像操作系统没有给您的程序访问此地址的权限(很可能已将其完全用于其他用途),操作系统引发了分段错误。 Read more here: What is a segmentation fault? 在此处阅读更多信息: 什么是细分错误?

If you really just wanted to print the value of the integer then use the correct printf command: 如果您真的只想打印整数值,请使用正确的printf命令:

int main(){
  int theNumber = 9009;
  printf("%d", theNumber);
}

Note that you don't need to introduce a string here to achieve this. 请注意,您无需在此处引入字符串即可实现此目的。

Okay. 好的。 Let's start by talking about what a string is in the C language. 让我们从讨论C语言中的字符串开始。 Fundamentally, it's a character pointer char * to a location in memory that stores a null terminated series of characters. 从根本上讲,它是一个字符指针char * ,它指向内存中存储以空终止的一系列字符的位置。

An example of this would be: 例如:

char *str = malloc(3);
str[0] = 'h';
str[1] = 'i';
str[2] = '\0';

str now contains the String "hi" . str现在包含字符串"hi"

What does a type cast do? 类型转换有什么作用?

The type casting that you are doing takes the integer 9009, and converts it to a char * . 您正在执行的类型转换采用整数9009,并将其转换为char * Then, since you use that pointer as a string, means that you are telling the computer that at address 9009, there is a null terminated series of bytes. 然后,由于您将该指针用作字符串,表示您正在告诉计算机地址9009处有一个空终止的字节序列。

That's probably not true though, and unless you are on specialized hardware, is certainly not what you want. 但是,那可能不是真的,除非您使用的是专用硬件,否则肯定不是您想要的。

How do you convert an integer to a string 如何将整数转换为字符串

We have a fairly easy mechanism to convert printf-able data to strings via snprintf() . 我们有一个相当简单的机制,可以通过snprintf()将可打印数据转换为字符串。

First we need to allocate memory for the resultant string. 首先,我们需要为结果字符串分配内存。 This is a common difference in C from other languages. 这是C语言与其他语言的共同差异。 In C, you are very aware of the memory requirements of your data. 在C语言中,您非常了解数据的内存要求。

char str[100];
int size_used = snprintf(str, 100, "%d", 9009);
assert(size_used < 100);

Now, if size_used >= 100 , then we have a problem. 现在,如果size_used >= 100 ,那么我们有问题。 We overflowed our memory area. 我们溢出了内存区域。 snprintf() will write as many characters as it can, but not the null terminating zero in that case. snprintf()会写入尽可能多的字符,但在这种情况下不会以null结尾零。

Because that is not a casting of an int to a char. 因为那不是将int强制转换为char。 It's a casting of an int to a pointer to char. 它是将int转换为指向char的指针。 Basically, a pointer is a variable that holds a memory address. 基本上,指针是保存内存地址的变量。 When you try to print that pointer, whose value is 9009, the function accesses the memory at that address, which probably is forbidden, and so you get the segfault. 当您尝试打印该指针(其值为9009)时,该函数将访问该地址处的内存,这可能是被禁止的,因此您会遇到段错误。

To avoid that, you should use something like itoa function (be careful that it's not standard, so you may have to implement it yourself). 为了避免这种情况,您应该使用类似于itoa函数的功能(请注意,它不是标准功能,因此您可能必须自己实现)。

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

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