Re: K&R2 , exercise 7.6



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.

.



Relevant Pages

  • Re: newbie program, pointers again...
    ... statements, a pointer to char ... int main ... you only copied the 7 characters, but no terminating null character. ...
    (comp.lang.c)
  • Re: random words
    ... char * phrase_generate ... You only have MAX characters, one of which is needed for a null terminating ... (You meant phrase, not frase - you defined it as phrase, earlier in the ...
    (comp.lang.c)
  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... Why is using char* a bad thing and why using sprintf a bad thing to, ... can be up to MAX_PATH characters). ... LPSTR lpMsgBuf; ... MessageBox(NULL, lpMsgBuf, "GetLastError() for ...
    (microsoft.public.vc.mfc)
  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... This means that if you develop the bad habit of using char * (left over ... It usually takes me five minutes to create a Unicode version of any of my apps, ... BOOL and bool are different data types. ... can be up to MAX_PATH characters). ...
    (microsoft.public.vc.mfc)
  • Re: Char and Varchar
    ... If the maximum length is short (<= 10 characters), ... maximum length, I also use CHAR. ... I use VARCHAR if long and short ...
    (microsoft.public.sqlserver.server)