[Courses] [C] Beginner's Lesson 4A: Errata - Correct your lesson!

KWMelvin kwmelvin at intrex.net
Fri Oct 11 07:27:09 EST 2002


On Thu, Oct 10, 2002 at 10:06:32PM -0700, Daniel. wrote:
> A few little quibbles and clarifications:
> 
> >The number of elements in the array is called the DIMENSION
> >of the array.
> 
> Hm, I don't think so. "float data[5]" is a one-dimensional array, not 
> a five-dimensional one. I'd call five the "length" of the array.

That sentence is almost verbatim from the book, however, I think
LENGTH is a better way to describe it!  Thanks!

> >	Strings are arrays of characters.  The special character `\0' (NUL)
> >	indicates the end of a string.
> 
> And is just a zero, by the way. Saying '\0' instead of 0 emphasizes 
> its function as a string-terminator to the human reader, but makes no 
> difference to the compiler.

Here I beg to differ!  The NUL character is NOT just a zero.  
Zero is ASCII code 0x30, and NUL is ASCII code 0x00.  Try this
little program to see what I mean.  After compiling it, run it
by piping the program's output to `od -a'.

	/* 0.c -- NUL is not just a zero */
	#include <stsio.h>
	int main(void)  /* explicitly declared */
	{
		char zero[] = "0";  /* the array has 2 elements, zero and NUL */
		printf("%c", zero[0]);
		printf("%c", zero[1]);
		return 0;
	}
	Compile:  gcc -o 0 0.c
	Run:  ./0 | od -a

`od' is a program that will dump files in octal and other formats.
The `-a' option tells od to dump the file in ASCII format, so we
can read it.  The output from doing the above looks like this with
the `-a' option (followed by the output from a hexadecimal dump `-h':
	$ ./0 | od -a
	0000000   0 nul
	0000002
	$ ./0 | od -h
	0000000 0030
	0000002

So here we can "see" that zero and NUL are two different characters!

> >	Note that string and character constants are very different:
> >	"X" is a one character string. 'Z' is a single character.
> 
> That is, "X" is an array with two elements: an 'X' and a '\0', in that order.

Yes! After running the above program, we can see that a string ends with
NUL, or `\0'.  So if our program above assigned "X" to the zero[] array,
the output from running the program and piping it to `od -a' would be:
	$ ./0 | od -a
	0000000   X nul
	0000002
	
> >	Function             Description
> >	-------------------+--------------------------------------
> >	strcpy(s1, s2)     | Copy s1 into s2.
> >	strcat(s1, s2)     | Concatenate s1 onto end of s2.
> 
> Other way around for both these; compare with the examples.

 My bad! Indeed, you are correct!  Here is the corrected table:

	Function             Description
	-------------------+--------------------------------------
	strcpy(s1, s2)     | Copy s2 into s1.
	strcat(s1, s2)     | Concatenate s2 onto end of s1.
 
 Thank YOU! I hope everyone is making these changes to their copies
 of the lessons!  We are fortunate that "many eyes" are reading
 these lessons and taking the time to point out my mistakes.
 
 
> >Note the ampersand in front of variable1. Don't forget it!
> 
> What it means will be explained later, of course.
> 
> -Daniel.

Thank you *very* much!  I especially appreciate the heads-up
about the strcpy and strcat entries being reversed.  Hopefully
everyone will read these corrections as they are posted!!!
All mistakes are mine!

Happy Programming!
--
K




More information about the Courses mailing list