I'm having this problem.
char buffer[100];
buffer[0] = "\n";
For some reason, the following statement is true
buffer[0] == 'T开发者_运维问答'
When it should be the "\n" ascii. Why?
"\n"
is a C string, that is a char *
pointing at a null terminated series of char
elements. Your program takes the address of that string, and stores the lowest 8 bits into buffer[0]
. In your case they happen to be the ASCII code for T
.
Try the following:
char buffer[100];
buffer[0] = '\n';
'\n'
is a char
literal, so this will behave as expected.
Edited: I got it wrong. Your code should be rewritten to the following code:
char buffer[100]={0};
buffer[0]= '\n';
Try *buffer[0] = '\n'
. I think that would give you the desired result, as it is char
you are assigning not string
. For string
use double quotes and for char
single quote.
As rightly pointed in the comment buffer[0]
is char pointer, so first it needs to be allocated memory too. calloc
would be a better choice here as it is going to assign default 0 values whereas malloc
will just allocate space having garbage values.
精彩评论