Re: K&R2 , exercise 7.6
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Mon, 21 Apr 2008 21:04:21 +0530
arnuld wrote:
On Mon, 21 Apr 2008 11:57:59 +0000, Richard Heathfield wrote:
I don't know whether you noticed, but I gave you a working version of
print_line - and you appear to have broken it again.
yes. I fixed it again but I am using putchar() now.
Wrong. In C, a string is a contiguous sequence of characters,
terminated by the first null character.
but these 2 are different:
char arr[] = "Richard";
char* pc = "Richard";
Yes. The first declaration creates an array (named arr) of the length
needed to store the string literal "Richard" which is of eight
characters including the terminating newline. Note that the elements of
arr are subsequently modifiable, as you have not const qualified it.
It's identical to any other char array, merely initialised in a
slightly special manner.
The second declaration says that pc is a pointer to type char and
initialises it to the address of the start of the string "Richard",
which is placed somewhere in memory according to the compiler's
convinience. The main difference with the first declaration is that
here pc is simply a char * and points to a string literal which cannot
be modified without invoking undefined behaviour. However pc can always
be set to point elsewhere in which case you'll lose access to the
string literal "Richard" unless you preserve it's address elsewhere.
both are contiguous sequence of characters ended by '\0'.
The first is a char array into which a string has been _copied_ . The
latter is a char * which points to a string literal somewhere in
memory.
I hope the difference is clear...
Wrong. printf interprets %s as meaning "the parameter matching this
format specifier is a ##pointer## to the first character in a
contiguous sequence of characters that is terminated by a null
character" - in other words, %s means "I'm giving you a string".
you can never use the pc for %s in printf(). Thats what I meant, there
are no strings, only arrays of chars. I tried to print the pc but all
I got was:
^A^M^D @#
You can perfectly print both the declarations you have given above using
the %s printf specifier. The %s format specifier looks for a char *
argument and prints all characters upto the first null character. You
can also use a length specifier to control the number of characters
printed.
.
- Follow-Ups:
- Re: K&R2 , exercise 7.6
- From: Keith Thompson
- Re: K&R2 , exercise 7.6
- References:
- K&R2 , exercise 7.6
- From: arnuld
- Re: K&R2 , exercise 7.6
- From: Richard Heathfield
- Re: K&R2 , exercise 7.6
- From: arnuld
- Re: K&R2 , exercise 7.6
- From: Chris Dollin
- Re: K&R2 , exercise 7.6
- From: arnuld
- Re: K&R2 , exercise 7.6
- From: Richard Heathfield
- Re: K&R2 , exercise 7.6
- From: arnuld
- K&R2 , exercise 7.6
- Prev by Date: Re: Time delay loop less than 1ms
- Next by Date: Re: Single RJ-45 with two NICs?
- Previous by thread: Re: K&R2 , exercise 7.6
- Next by thread: Re: K&R2 , exercise 7.6
- Index(es):
Relevant Pages
|