#include <stdio.h>
#include <stdlib.h>
int main()
{
char a="9jhjhi";
printf("%s",开发者_JS百科a);
}
Why does this throw a segmentation fault? What happens behind the screens?
You need to use char *a = "..."
.
printf
when passed %s
will run through a string looking for a 0
/NULL
byte. 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.
#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.
%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. 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.
You should also return something from the main() method - it's declared to return an int after all.
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). 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.
Use char *a = ...
.
C does not have strings as in String b = new String();
C has arrays of type char.
So char a="123123" should be a character array.
You aren't using anything from stdlib.h in that code either so there is no reason to #include it.
Edit: yeah, what nos said too. An array name is a pointer.
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:
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
.
Then the char
is sent into printf
as a number, but interpreted as a string. Since it's not null-terminated in any way, printf
probably overruns memory when racing through that "string".
When you declare char a
without a pointer symbol, you are only allocating enough space on the stack for a single character.
Strings in C are represented by a char
array, or a pointer to a char
array, terminated by the null character '\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. Use char array (char[size]) instead. You may also want to terminate the string with "\0".
You could remove this error in two ways.
1.char * p="karthik A"
2.char [ ]p="karthik A"
精彩评论