More Pascal to C Issues (26 Views)
Reply
Frequent Advisor
Greg White
Posts: 78
Registered: ‎10-02-2001
Message 1 of 7 (26 Views)
Accepted Solution

More Pascal to C Issues

Hello,

A. Clay Stephenson gave me an example of how to allocate a variable sized array in C but his example was for a one-dimensional array. Is this also possible with two-dimensional arrays?

I like to understand the big picture before I start on a project this big.

Thanks in advance.
I know how to do it in pascal.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 2 of 7 (26 Views)

Re: More Pascal to C Issues

Hi Pascal:

I spent about 5 minutes putting together the 2 dimensional version. In fact, this technique can be extended to n dimensions. The problem is that this gets pretty deep pretty quickly.

In this case I set up an array of records, arry2[50][100];

The key is that arry2 is declared as my_rec **arry2 but USED in an array context. What you do is first allocate the rows and then allocate the columns associated with each row.

We still haven't covered the case of truly dynamic arrays. In this case, the arrays are set up but do not change beyond that. It allows you to size arrays from the command line or from a configuration file but does not allow you to expand them as needed. That too is possible but I'm already beginning to feel that I am teaching C again.

Anyway, here's the attached code.

Clay
If it ain't broke, I can fix that.
Frequent Advisor
Greg White
Posts: 78
Registered: ‎10-02-2001
Message 3 of 7 (26 Views)

Re: More Pascal to C Issues

Thank you! I can't believe you did that in 5 minutes. I would have spent hours doing that.
I know how to do it in pascal.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 4 of 7 (26 Views)

Re: More Pascal to C Issues

Hi Pascal:

I confess I did cheat a little bit. I basically yanked that out of some existing code and added the part dealing with looping through the array. Since you mentioned dynamic arrays, I've been racking my brain trying to remember how I used to do that in Pascal. I remember something about using a free union and turning range checking off around any code that accessed the array by I can't remember much beyond that.

Clay
If it ain't broke, I can fix that.
Frequent Advisor
Greg White
Posts: 78
Registered: ‎10-02-2001
Message 5 of 7 (26 Views)

Re: More Pascal to C Issues

Hello Clay,

Thank you for your two-dimensional array example. I can't seem to do the allocation in a function. I have about ten arrays of the same record type but withing different size. I know this is something simple but these ******** C pointers are insane. Can you show me how to do this?

I know how to do it in pascal.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 6 of 7 (26 Views)

Re: More Pascal to C Issues

Hi Pascal:

Can I show you how to do this? Does a hog like slop?

Actually C pointers are not insane, it's just that in Pascal you have pass by value and pass by reference (VAR) so that the underlying details were hidden. In Pascal, you could go all the way through a first semester course and never see a pointer; in C you might go to or two days.

The key here is *** my_rec - a pointer to a pointer to a pointer. I've modified the earlier code to include a integer function allocate_array. 0 means all went well. You specify the number of rows and columns and the *** my_rec. I've commented the call so you see to use the & operator in the function call. This is similar to a boolean function in Pascal with one of the VAR params being set as a side effect. The other method is to let the function return the array itself or NULL on failure. I'll do that variant if I have time.

Clay
If it ain't broke, I can fix that.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 7 of 7 (26 Views)

Re: More Pascal to C Issues

Hi again:

Here is the other method that returns a pointer to the array. I really prefer the previous method since the status is returned as well as a pointer. The code is a bit clearer in this example.

Clay
If it ain't broke, I can fix that.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.