From: theory.TC.Cornell.EDU!lijewski@tcgould.tn.cornell.edu (Mike Lijewski)
Subject: Re: question on constant arrays
Date: 5 Feb 93 14:10:54 GMT [thread overview]
Message-ID: <1993Feb5.141054.29026@tc.cornell.edu> (raw)
In article <EMERY.93Feb4205855@dr_no.mitre.org> emery@dr_no.mitre.org (David Em
ery) writes:
>The exact Ada analog to the C record is as follows:
> type strptr is access string;
> type mytype is record
> name : strptr;
> T : integer;
> end record;
>
> type foo_array is array (natural range <>) of strptr;
> foo : foo_array(1..2) := (new string'("Hi"), 1),
> new string'("There"),2));
>
>In the C example, the compiler statically allocates space for the two
>string literals, and returns their address. In the Ada case, you have
>to explictly allocate storage for the two strings, and use the
>access value.
>
>Remember that in C, all strings are passed by the address of their
>first element. This is not the way that Ada handles strings.
>
> dave
>p.s. I suspect that you'll be in deep trouble with your C array of
>struct if you try to use any of the standard C techniques to iterate
>over the characters in the Name field. Does C guarantee that string
>literals like "Hi" are null terminated?
C does guarantee that string literals are null terminated. The one
"oddity" allowed by ANSI C is where one initializes a fixed size character
array, which is just large enough to hold the characters in the string
literal excluding the null, with a literal string. That is,
char word[2] = "Hi"; /* word is not null terminated */
as contrasted to letting the compiler figure out the size of the array:
char word[] = "Hi"; /* word is null terminated */
Why ANSI allowed this I don't know. C++ disallows this special case.
--
Mike Lijewski (H)301-982-5461 (W) 301-286-6115
Goddard Space Flight Center
ARPA: lijewski@rosserv.gsfc.nasa.gov
SMAIL: 446 Ridge Rd. Apt. 3, Greenbelt, MD 20770
next reply other threads:[~1993-02-05 14:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1993-02-05 14:10 Mike Lijewski [this message]
-- strict thread matches above, loose matches on Subject: below --
1993-02-05 1:58 question on constant arrays David Emery
1993-02-04 4:48 agate!spool.mu.edu!olivea!charnel!rat!koko.csustan.edu!nic.csu.net!vmsa.i
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox