TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Alan Clifford
from: George White
date: 1998-11-01 09:45:00
subject: character arrays tha

Hi Alan,

You wrote to me:

AC> DC>>> my book on C only talks about predefined character strings, ie:
AC> DC>>> static char *books[30] = {"moby dick",
"annie", etc}; But not on how
AC> DC>>> to read data into one.

AC> WM>> That's an array of pointers to constant strings. You
can't read into
AC> WM>> it.

AC> GW> Not so :-(. There is no "const" in there...
AC> GW> That is an initialised array of strings of varying length that retains
AC> GW> it's value between calls to the function it's declared in. You _can_
AC> GW> write to them, but it's a dangerous occupation since they have the
AC> GW> length of the string they were initialised with and so vary in
AC> GW> length.


AC>Surely *books[30] is an array of thirty pointers to char.

Surely it is :-)

AC>I know my dos compilers will allow the strings to be
AC>changed but "moby dick" is a string literal.

K&R (I've only got the 1st ed) are confusing on this too. In the text
(sec 2.3 "Constants", & 5.5 "Character Pointers and
Functions") they say
"I am a string" is a string constant, but in Appendix A "C Reference
Manual" they say:

"2.5 Strings

 A string is a sequence of characters surrounded by double quotes, as in
"...". A string has type `Array of characters' and storage class static
(see 4 below) and is initialised with the given characters."



So, I'm going by the latter definition in saying "That is an initialised
array of strings of varying length that retains it's value between calls
to the function it's declared in." and saying the string data can be
changed. In this case what is created is initialised strings, not
string constants.

George

 * SLMR 2.1a * Computers eliminate spare time.

--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-717904) (2:440/4)
SEEN-BY: 396/1 632/0 371 633/260 267 270 371 634/397 635/506 728 639/252
SEEN-BY: 670/218
@PATH: 440/4 255/1 251/25 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.