Re: chars
- From: jameskuyper@xxxxxxxxxxx
- Date: Tue, 8 Jan 2008 12:12:51 -0800 (PST)
Bill Cunningham wrote:
"James Kuyper" <jameskuyper@xxxxxxxxxxx> wrote in message....
news:WgCgj.1965$sH.1240@xxxxxxxxxxx
...."12345\0" is a pointer to the first element of an array of char containing
the following values: {'1', '2', '3', '4', '5', '\0', '\0'}.
The first six characters of that array form a string. However, the array
as a whole is not a single string; it could arguably be called two
consecutive strings.
This null terminating string the user never puts into his string right?
That sentence doesn't quite make sense. Null-terminated strings are
terminated, as the name implies, by null characters. A C string
literal has a value which is a pointer at the first element of an
array of char which is automatically null-terminated. A string literal
it can also contain explicit null characters, as in your example.
That's why the corresponding array has two null characters a the end,
not just one.
The compiler, assembler, or linker puts it there everytime right? I have
seen some source where the programmer wants the compiler to put a null
string into the code hence,
char a;
a='\0';
So a contains the value of the null pointer between ' '. If I am reading
correctly.
You're confusing null characters with null pointers, a confusion that
C compounds because a null character constant IS a null pointer
constant, if it appears in a pointer context. This is NOT a pointer
context, so '\0' is a integer constant with a value of 0.
char *pc = '\0'; // pointer context: null pointer constant
*pc = '\0'; // non-pointer context: integer constant
You also seem to be confusing character literals with string literals.
'\0' is a character literal with a value of 0. "hello" is a string
literal. The value of that string literal is a pointer to an array of
six chars, the last of which is a null character. "he\0llo" is a
string literal which points to an array of 7 chars. In that array,
both the 3rd char and the 7th one are null characters. "\0" points to
an array of two chars, both of then null characters. "" points to an
array of 1 char, which is a null character.
.
- Follow-Ups:
- Re: chars
- From: Bill Cunningham
- Re: chars
- References:
- Prev by Date: Casts for srand? (FAQ 13.17)
- Next by Date: Re: Casts for srand? (FAQ 13.17)
- Previous by thread: Re: chars
- Next by thread: Re: chars
- Index(es):
Relevant Pages
|