David Bowskill wrote:
Your email has been very helpful to me - thanks again. When I initially copied the program from Herbert Schildes book

Well that explains why you're incrementing characters in strings. I was wondering (it's not really a good idea because of internationalisation).

Herbert Schildt's books aren't fantastic -- he has written a lot,
he explains things poorly and they don't get updated that often.
And the enforcement of strings literals being constant is only
recently come into vogue. For a long time it wasn't seen as a
good idea, but in this age of buffer overflows you want everything
that can be a constant to be a unalterable.

The library function strdup() is not mentioned in K&R but strcpy() is - I assume that these are very similar.

It's in UNIX98, not ANSI/ISO C. Sorry about that. Off the top of my head

char *strdup(const char *str)
{
    char *new;

    new = (char *)malloc(strlen(str) + 1);
    if (new) {
        strcpy(new, str);
    }
    return new;
}

[Once you've learnt C have a look at PJ Plauger's book on implementing
 the standard C library.  There's no better way to learn to use a
 library than to be walked through an implementation by a wonderful
 author.]

There is another question which no doubt you can help on - I have read that the library function 'gets' (defined in K&R) is regarded as dangerous in Linux - is this true?

It's unsafe in any operating system. Take this example to read a string of up to 10 characters:

  char s[10+1];
  success = gets(s);
  if (!success) { ...

now what's going to happen when the user feeds it 20 characters?
It's going to try to write outside the bounds of the allocated
string.

You want to say this:

  #define S_SIZE (10 + 1)
  char s[S_SIZE];
  success = fgets(s, S_SIZE, stdin);
  if (!success) {
    perror("Failed reading hotel name");
    exit(EXIT_FAILURE);
  }

Note how I handled the error. Every I/O is checked for errors,
and the error message is in the user's terminology.

Also, do you know of an IDE for GCC ( in a similar manner to that provided by Borland C++) ? This would be very useful.

There are endless numbers of them, since most UNIX editors have good programming support. For example, in Emacs you create a Makefile containing the line t: t.c and type M-x compile (and the second thing you'll do in emacs is assign that command to a function key).

There is an excellent Borland-like IDE for beginners, and if
you hunt around www.freshmeat.net you'll probably find it.

Brett Nash wrote:
As an aside, if you don't have a good reference on C, I would suggest
hunting down a copy of Brian Kernighan & Dennis Richies "The C
Programming Language", a little expensive (~$70) for it's size
(250pages), but you won't find a better reference ANYWHERE for C, with
the possible exception of the ANSI standard, and K&R is much more
readable.  The reference manual section will allow you work out such
issues very quickly (A2.6 covers string literals, and your specific
problem quite concisely - I can post you the section if you wish).

Don't bother with the ANSI/ISO standard, they aren't meant to be textbooks. Standards necessarily have to deal with the odd and difficult to explain cases, and sometime need to be handwaving when the beginner needs strong advice (eg, overwriting a constant, standard says "undefined", textbook says "don't do that").

You should be able to pick up K&R second-hand with little trouble.
Only bother with the 2nd edition if you are learning C.

A Book on C is a classic beginners text.  Microsoft Press had
a nice reference guide, but I think they reissued it as a book
with a fair mark-up in the price.  If you move to C++ or Java
then Bruce Eckel's books are outstanding and online.

--
 Glen Turner         Tel: (08) 8303 3936 or +61 8 8303 3936
 Australia's Academic & Research Network  www.aarnet.edu.au
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to