home *** CD-ROM | disk | FTP | other *** search
- From: richw@inmet.camb.inmet.com
- Date: 12 Aug 1992 10:48 EDT
- Newsgroups: comp.lang.c
- Subject: Re: Computer terms, etc
- Message-ID: <20900024@inmet>
- Path: sparky!uunet!inmet!inmet!richw
- Nf-ID: #R:<cee1:713424953:inmet:20900024:000:1740
- Nf-From: inmet.camb.inmet.com!richw Aug 12 10:48:00 1992
- References: <713424953@<cee1>
- Lines: 63
-
-
- >> char *foo = "abc";
- >> char *foo[] = "abc";
- >> char foo[] = "abc";
- >> char foo[4] = "abc";
-
- First, let me give these "foo"s unique names:
-
- char *fooA = "abc";
- char *fooB[] = "abc";
- char fooC[] = "abc";
- char fooD[4] = "abc";
-
- Here's a way of rewriting these which might make things clearer:
-
- static char literal [] = { 'a', 'b', 'c', '\0' };
-
- /* "literal" is now a constant which equals the address */
- /* of the first element of an initialized, 4-element */
- /* "char" array */
-
- char *fooA = literal;
- char *fooB[] = (char **) literal;
- #define fooC literal
- #define fooD literal
-
- Thus, "fooC" and "fooD" are identical; both are constants which equal the
- address of the "literal" array's first element.
-
- "fooA", on the other hand, is an initialized variable which can be re-
- assigned, i.e. the following are legal:
-
- char *ptr;
-
- fooA = "some other literal";
- fooA = ptr;
-
- but this isn't:
-
- fooC = "some other literal";
-
- Note that the "4" in the "char fooD[4]" declaration is entirely unnecessary;
- what's interesting are these cases:
-
- fooD[3] = "abc"; /* Illegal ! "fooD" is too small */
-
- fooD[5] = "abc"; /* "fooD" is the constant address */
- /* of the first element of a 5- */
- /* element "char" array whose 5-th */
- /* element is uninitialized */
-
- Finally, note that "char *fooB[]" represents a pointer to an array, where
- each element of the array is itself a pointer to "char". This is a
- *different* type than the type of "abc", i.e. a pointer to "char". I'm
- surprised my C compiler accepted for "fooB" declaration. In any case,
- I would never write such a declaration...
-
-
- Rich Wagner
- Intermetrics, Inc.
- richw@inmet.camb.inmet.com
-
-
-