home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: extern: interesting case...
- Message-ID: <1993Jan8.073635.354@sq.sq.com>
- Summary: some basics about pointers and arrays
- Keywords: extern
- Organization: SoftQuad Inc., Toronto, Canada
- References: <bibhas.726341439@femto.engr.mun.ca> <4311@dozo.and.nl>
- Date: Fri, 8 Jan 93 07:36:35 GMT
- Lines: 35
-
- > char line[80];
- > char *linep= line;
- > tells the compiler that the object named `linep' is a pointer, pointing
- > to an 80 bytes wide piece of memory that can also be refered to as `line'.
-
- Actually, `linep' points to the FIRST of the 80 bytes. However, because
- of the way arrays and pointers work in C, that is exactly what you want
- if you intend to access the other bytes as well, one by one. (See Section
- 2 of the comp.lang.c FAQ list, and/or any reasonable quality C book.)
- In fact, this is so true that people sometimes slip up (or speak loosely)
- and *say* that the pointer is pointing to the whole array, as in the
- text quoted above. But it really is not.
-
- The reason it points to a BYTE is that its type is "char *" and a char
- is always one byte in C. (This does not necessarily mean 8 bits; it
- could be more.) If we had
-
- int thing[80];
- int *thingp = thing;
-
- and if ints are, say, 2 bytes long, then thing is 160 bytes, and thingp
- is pointing simultaneously to the first TWO of them.
-
- It is probably better to NOT think of pointers in terms of bytes, but
- in terms of objects. Now thing contains 80 ints, and thingp points to
- the first of these ints. See, now there's no difference between int *
- and char *, except the substitution of "int" for "char". And you don't
- need to know the number of bytes in an int to understand the code.
- --
- Mark Brader "It's important not to let the structure of the
- Toronto code determine the functionality of the program ...
- utzoo!sq!msb The desired functionality should be predetermined
- msb@sq.com before the code writing is done." -- Dave Sill
-
- This article is in the public domain.
-