home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: Ragged arrays of type long?
- Message-ID: <3241@dozo.and.nl>
- Date: 17 Aug 92 15:04:39 GMT
- References: <1992Aug15.052649.20308@CS.ORST.EDU>
- Organization: AND Software BV Rotterdam
- Lines: 62
-
- In article <1992Aug15.052649.20308@CS.ORST.EDU> warninm@xanth.CS.ORST.EDU (Michael Warning) writes:
- |Is it possible to create a ragged array of type long? My references goes on
- |for two pages about how neat ragged arrays of strings are, but says nothing
- |about any other data type.
- |
- |For example:
- |
- | static char *try1[] = {"abc",
- | "wxyz"};
- |
- |This works fine, but:
- |
- | static long *try2[] = {{1, 2, 3},
- | {5, 6, 7, 8}};
- |
- |This causes the various flavors of MS-C that i've tried to come up with a
- |'Different levels of indirection' error. [ ... ]
-
- It is a bit tricky I must admit. It seems that the standard made a bit
- of an exception to the rule (but they didn't). Let me try to explain:
-
- pointer types and arithmetic types are both called `scalar' types.
- Arithmetic types are split up in `floating point' types and `integer'
- types. Somewhere in the standard it reads:
-
- 3.5.7 Initialization
-
- The initializer for a scalar shall be a single expression, optionally
- enclosed in braces. [ ... ]
-
- Every element of your try2 array is a scalar type object, i.e. a pointer
- to a long. So, every element can be initialized with just one initializer.
- A string literal is considered _one_ initializer. Just take a look at
- this, maybe it clarifies things a bit:
-
- char *p = "abc";
-
- is perfectly legal, but
-
- char *p = { 'a', 'b', 'c', '\0' };
-
- is not. It only works for an agregate type (an array.)
-
- The (ugly) trick you can do here is: introduce separate arrays,
- one for every row of the try2 array. Something like this would do it:
-
- static long try2_row0[] = { 1, 2, 3 };
- static long try2_row1[] = { 5, 6, 7, 8 };
-
- Defining those arrays without an explicit dimension causes the comiler
- to allocate just enough memory for those array, i.e. 7 slots. Hook
- them all together like this:
-
- static long *try2[] = { try2_row0, try2_row1 };
-
- And presto: there's your `ragged' array.
-
- I hope this helps a bit,
-
- kind regards,
-
- Jos aka jos@and.l
-