From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,5deaed13ab94dd56 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 15 Nov 2005 13:53:56 -0600 From: tmoran@acm.org Newsgroups: comp.lang.ada Subject: Re: Arrays & pointers question References: <1132082934.714260.258260@g43g2000cwa.googlegroups.com> X-Newsreader: Tom's custom newsreader Message-ID: Date: Tue, 15 Nov 2005 13:53:56 -0600 NNTP-Posting-Host: 67.169.16.9 X-Trace: sv3-ZXYALo28SFKDo5jvopegcYowFzfst0r/tgkRFlIWft0cCK8ZngBC11Aca5qL3nt7H3+VZrLoygATR5f!RlFLIJteIrK+cCOxhM42svd+ASvFDOQm560l/fFfGPMJLMNyRtL9QIZgf/Cjp1K8JoT+QluLPCY= X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news1.google.com comp.lang.ada:6404 Date: 2005-11-15T13:53:56-06:00 List-Id: > type MYTYPE is array (1 .. 10) of INTEGER; > X1: MYTYPE; > X2: MYTYPE; > X3: MYTYPE; > > I have a SORT procedure that takes 2 parameters: the first is the > number of entries, > and the 2nd is the address of the array. > > What's the correct syntax for the procedure declaration, the procedure > call, and the references to the array elements inside the procedure? Arrays in Ada are not pointers or addresses but rather objects, so you need to pass the object in, sort it, and pass it out (don't worry, the compiler is smart enough to generate code that passes references even though logically it's copying the array). Also, arrays carry their own upper and lower boundaries, so you don't need to pass a count. So declare your procedure this way: procedure Sort(This_Array : in out MyType) is begin for i in This_Array'range loop for j in This_Array'first .. i-1 loop if This_Array(i) < This_Array(j) then .... ... and call it like this: Sort(X1); Note that in this case you've declared any array of type MyType to have exactly 10 elements, so the compiler doesn't even need to pass the count, and it can generate code knowing that This_Array'range is 1 .. 10 and This_Array'first is 1. If you want to sort different length arrays, try type MYTYPE is array (Integer range <>) of INTEGER; X1: MYTYPE(1 .. 10); X2: MYTYPE(1 .. 20000); The declaration and call of Sort are unchanged. Since the parameter to Sort has bounds specified at run time, you also have the option of sorting just a slice of a MYTYPE array, eg the first ten elements of X2: Sort(X2(1 .. 10)); or the last ten elements: Sort(X2(19991 .. 20000));