comp.lang.ada
 help / color / mirror / Atom feed
* question on constant arrays
@ 1993-02-04  4:48 agate!spool.mu.edu!olivea!charnel!rat!koko.csustan.edu!nic.csu.net!vmsa.i
  0 siblings, 0 replies; 3+ messages in thread
From: agate!spool.mu.edu!olivea!charnel!rat!koko.csustan.edu!nic.csu.net!vmsa.i @ 1993-02-04  4:48 UTC (permalink / raw)


    Is it possible to declare an array of record types, with a string
within the record say like in 'C': 
    struct mytype {
    	char *Name;
    	int T;
    };
    struct mytype foo[] = {{"Hi", 1}, {"There!", 2}};

    I have tried:
    	type MY_TYPE(Size : positive := 1) is
    	    record
    		Name : STRING(1..Size);
    		T    : INTEGER;
    	    end record;

    	type MY_ARRAY is array(positive range <>) of MY_TYPE;
    	Foo : constant MY_ARRAY := ((size => 2, Name => "Hi", T => 1)....) ?

    and the compiler balked at me. It seems that you can do:
    	NAME : constant STRING := "Hi";    	
    and since type STRING is declared to be: 
    	type STRING is array (positive range <>) of CHARACTER;
    the compiler shouldn't have any problems figuring out how
    many MY_TYPE records I am actually putting in my constant
    array. If it could figure it out for strings, why not for 
    MY_TYPE? Or is the problem with MY_TYPE's 'Size' need to 
    be bound before hand? Or is it because the compiler 
    'knows' that my project is due in 2 days?

    	Thanks in advance for any info.


    					Gabriel
    					marasigan@csupomona.edu

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: question on constant arrays
@ 1993-02-05  1:58 David Emery
  0 siblings, 0 replies; 3+ messages in thread
From: David Emery @ 1993-02-05  1:58 UTC (permalink / raw)


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?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: question on constant arrays
@ 1993-02-05 14:10 Mike Lijewski
  0 siblings, 0 replies; 3+ messages in thread
From: Mike Lijewski @ 1993-02-05 14:10 UTC (permalink / 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 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1993-02-05 14:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-02-04  4:48 question on constant arrays agate!spool.mu.edu!olivea!charnel!rat!koko.csustan.edu!nic.csu.net!vmsa.i
  -- strict thread matches above, loose matches on Subject: below --
1993-02-05  1:58 David Emery
1993-02-05 14:10 Mike Lijewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox