[英]segmentation fault in C assign string to char
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a="9jhjhi";
printf("%s",a);
}
Why does this throw a segmentation fault? 为什么这会引发细分错误? What happens behind the screens?
屏幕后面会发生什么?
You need to use char *a = "..."
. 您需要使用
char *a = "..."
。
printf
when passed %s
will run through a string looking for a 0
/ NULL
byte. 传递
%s
时, printf
将通过字符串查找0
/ NULL
字节。 In your case you are not assigning a string literal to a
, and in fact your compiler should have thrown a warning that you were trying to initialize a char
from a pointer. 在你的情况你是不是指定一个字符串来
a
,而实际上你的编译器应该扔了你试图初始化警告char
从一个指针。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *a="9jhjhi";
printf("%s",a);
}
Your error is that 你的错误是
char a="9jhjhi";
should be 应该
char *a="9jhjhi";
What happens is undefined behavior - so anything could happen. 发生的是未定义的行为-因此任何事情都可能发生。
Your assigning a string literal to a char, so your a
will contain a pointer(to the beginning of that string) converted to a char - whatever that'll be. 您将字符串文字分配给char,因此
a
将包含一个指针(指向该字符串的开头),该指针将转换为char-无论如何。
%s
conversion in printf assumes you pass it a string, which must be a char* pointing to a sequence of chars ending with a 0 terminator. printf中的
%s
转换假定您向其传递了一个字符串,该字符串必须是char *,它指向以0终止符结尾的字符序列。 You passed it a char, which certainly does not meet those requirements, so it's quite undefined what'll happen - a crash could be common. 您为它传递了一个char,它肯定不满足那些要求,所以将要发生的事情是非常不确定的-崩溃很常见。
You should also return something from the main() method - it's declared to return an int after all. 您还应该从main()方法返回一些内容-声明它毕竟要返回一个int值。
C does not have strings as in String b = new String(); C没有字符串b = new String();
C has arrays of type char. C具有char类型的数组。
So char a="123123" should be a character array. 因此char a =“ 123123”应该是一个字符数组。
You aren't using anything from stdlib.h in that code either so there is no reason to #include it. 您也没有在该代码中使用stdlib.h中的任何内容,因此没有理由#include它。
Edit: yeah, what nos said too. 编辑:是的,也没有说什么。 An array name is a pointer.
数组名称是一个指针。
a
is initialized to a (cast to integer and truncated because char
is 3 or 7 bytes too small) pointer that points to a char array (propably somewhere in ROM). a
初始化为一个指向char数组(可能在ROM中的某个位置)的指针(将其转换为整数,并由于char
太小3或7个字节而被截断)。 What follows is undefined, but it's propably like this: When you pass it to printf
with a %s
in the format string, it takes the value of a
(something in 0-255
) and 3 (or 7) unrelated bytes from the stack, gets some bogus address and wreaks havok by accessing someone else's memory. 接下来的内容是未定义的,但可能是这样的:当您将它传递给带有格式字符串中的
%s
printf
时,它将从堆栈中获取a
(在0-255
)和3(或7)个无关字节的值。 ,获取一些虚假的地址,并通过访问别人的记忆来发ha。
Use char *a = ...
. 使用
char *a = ...
You mean 你的意思是
char *a = "9jhjhi";
If this compiles without warnings, your compiler settings are messed up. 如果编译时没有警告,则说明您的编译器设置被弄乱了。 The warnings from gcc show plainly what's happening:
来自gcc的警告清楚地表明了正在发生的事情:
test.c: In function ‘main’:
test.c:5: warning: initialization makes integer from pointer without a cast
test.c:6: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
The string literal is interpreted as a pointer, which is converted (and truncated) to a char
. 字符串文字被解释为一个指针,该指针被转换(并被截断)为
char
。
Then the char
is sent into printf
as a number, but interpreted as a string. 然后,将
char
作为数字发送到printf
,但将其解释为字符串。 Since it's not null-terminated in any way, printf
probably overruns memory when racing through that "string". 由于它绝不是以Null结尾的,因此在通过该“字符串”进行竞争时,
printf
可能会超出内存。
When you declare char a
without a pointer symbol, you are only allocating enough space on the stack for a single character. 当声明不带指针符号的
char a
,您仅在堆栈上为单个字符分配了足够的空间。
Strings in C are represented by a char
array, or a pointer to a char
array, terminated by the null character '\\0'
. 在C字符串由代表
char
阵列,或一个指向一个char
阵列,由空字符结束'\\0'
。 But if you use a literal string, the compiler takes care of all of that for you. 但是,如果您使用文字字符串,则编译器会为您处理所有这些工作。
Here's how you can get your code to work, then: 这是使代码正常工作的方法,然后:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = "9jhjhi";
printf("%s",a);
}
First of all, you're trying to save an entire string into a single char variable. 首先,您试图将整个字符串保存到单个char变量中。 Use char array (char[size]) instead.
请改用char数组(char [size])。 You may also want to terminate the string with "\\0".
您可能还想以“ \\ 0”终止字符串。
You could remove this error in two ways. 您可以通过两种方式消除此错误。
1.char * p="karthik A" 1.char * p =“ karthik A”
2.char [ ]p="karthik A" 2.char [] p =“ karthik A”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.