Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Looking for C's equivalent of Pascal's "with"

Reply
Thread Tools

Looking for C's equivalent of Pascal's "with"

 
 
Ruud Baltissen
Guest
Posts: n/a
 
      10-02-2013
Hello,


The Pascal equivalent of struct is record. It is possible to nest records inside other records, just like with structs. Assume record Drive0 is nested inside record Device and has the elements InUse and PCfile:

Device = record
...
...
Drive0 = record
InUse : boolean;
PCfile : string;
...
end;
...
end;

If I want to access PCfile of Drive0 I could do it like this:
Device.Drive0.PCfile := "D:\disk.ima";
In C it would look about the same. But in Pascal there another way:

with Device do
begin
...
...
with Drive0 do
begin
InUse := True;
PCfile := "D:\disk.ima";
end;
end;

This comes in handy when handling a lot of elements in one go, for example: when initializing a drive.

I'm looking for a C equivalent of this "with" construction. All programming examples I saw so far always showed the complete structure. Is there an equivalent or not?

Thank you for your time and info!


Kind regards, Ruud Baltissen
www.Baltissen.org
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-02-2013
On 10/2/2013 9:35 AM, Ruud Baltissen wrote:
> Hello,
>
>
> The Pascal equivalent of struct is record. It is possible to nest records inside other records, just like with structs. Assume record Drive0 is nested inside record Device and has the elements InUse and PCfile:
>
> Device = record
> ...
> ...
> Drive0 = record
> InUse : boolean;
> PCfile : string;
> ...
> end;
> ...
> end;
>
> If I want to access PCfile of Drive0 I could do it like this:
> Device.Drive0.PCfile := "D:\disk.ima";
> In C it would look about the same. But in Pascal there another way:
>
> with Device do
> begin
> ...
> ...
> with Drive0 do
> begin
> InUse := True;
> PCfile := "D:\disk.ima";
> end;
> end;
>
> This comes in handy when handling a lot of elements in one go, for example: when initializing a drive.
>
> I'm looking for a C equivalent of this "with" construction. All programming examples I saw so far always showed the complete structure. Is there an equivalent or not?


There's nothing that lets you omit the entire "provenance"
of a struct or sub-struct. However, you can use a pointer
variable to collapse or condense some outer nesting levels.
For example, here's a struct containing sub-struct elements:

struct inner {
int this;
int that;
};

struct outer {
struct inner one;
struct inner two;
struct inner three;
} instance;

One way to access the members is to write `instance.one.this'
and `instance.three.that' and so on. Another is to aim a
pointer variable at the particular inner struct you care about
at the moment, and use that instead:

struct inner *p = &instance.two;
p->this = 42;
p->that = 17;

This technique can collapse additional levels if desired:

struct atomic {
double trouble;
short snort;
};

struct inner {
int this;
int that;
struct atomic redThing;
struct atomic blueThing;
};

struct outer {
struct inner one;
struct inner two;
struct inner three;
} instance;

struct atomic *q = &instance.three.redThing;
q->trouble = 3.17;
q->snort = 0;

.... instead of `instance.three.redThing.trouble = ...' and so on.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      10-02-2013
"Ruud Baltissen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> If I want to access PCfile of Drive0 I could do it like this:


> Device.Drive0.PCfile := "D:\disk.ima";


> In C it would look about the same. But in Pascal there another way:
>
> with Device do
> begin
> ...
> ...
> with Drive0 do
> begin
> InUse := True;
> PCfile := "D:\disk.ima";
> end;
> end;
>
> This comes in handy when handling a lot of elements in one go, for
> example: when initializing a drive.


You can use something like this

struct S drive0 = {.InUse =True, .PCfile = "D:\\disk.ima"};

(although I've never used it myself). I don't know how it works if drive0 is
nested; you either have to initialise the whole Device struct, or create
drive0 as above then just assign it: Device.Drive0 = drive0;

(I think that can done in one go in with certain C systems.)

--
Bartc



 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      10-02-2013
On Wed, 02 Oct 2013 06:35:33 -0700, Ruud Baltissen wrote:

> I'm looking for a C equivalent of this "with" construction. All
> programming examples I saw so far always showed the complete structure. Is
> there an equivalent or not?


No.

However, given a structure, you can obtain a pointer to any field within
that structure, then access fields via that pointer. So when dealing with
nested structures, it's not uncommon to take a pointer to some
sub-structure and assign it to a variable. You can't make fields appear as
variables, but you can collapse the top-level variable name and any number
of intervening levels into a single variable.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-02-2013
"BartC" <(E-Mail Removed)> writes:

> "Ruud Baltissen" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> If I want to access PCfile of Drive0 I could do it like this:

>
>> Device.Drive0.PCfile := "D:\disk.ima";

>
>> In C it would look about the same. But in Pascal there another way:
>>
>> with Device do
>> begin
>> ...
>> ...
>> with Drive0 do
>> begin
>> InUse := True;
>> PCfile := "D:\disk.ima";
>> end;
>> end;
>>
>> This comes in handy when handling a lot of elements in one go, for
>> example: when initializing a drive.

>
> You can use something like this
>
> struct S drive0 = {.InUse =True, .PCfile = "D:\\disk.ima"};
>
> (although I've never used it myself). I don't know how it works if drive0 is
> nested; you either have to initialise the whole Device struct, or create
> drive0 as above then just assign it: Device.Drive0 = drive0;


The example given had "..." after the two members explicitly shown.
That suggested to me that assignment of the whole thing would probably
be more trouble that it's worth.

> (I think that can done in one go in with certain C systems.)


--
Ben.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      10-02-2013
"Ben Bacarisse" <(E-Mail Removed)> wrote in message
news:0.9cd3c2c400b6cec47c7b.20131002164029BST.87li (E-Mail Removed)...
> "BartC" <(E-Mail Removed)> writes:
>
>> "Ruud Baltissen" <(E-Mail Removed)> wrote in message


>>> Device.Drive0.PCfile := "D:\disk.ima";


>> struct S drive0 = {.InUse =True, .PCfile = "D:\\disk.ima"};


> The example given had "..." after the two members explicitly shown.
> That suggested to me that assignment of the whole thing would probably
> be more trouble that it's worth.


Possibly, but at least it would replace the repetition of 'Device.Drive0'.

I suppose the OP can also try this:

#define DD Device.Drive0

DD.InUse = True;
DD.PCfile = "D:\\dis.ima";

#undef DD

which at least involved less typing.

--
Bartc

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-03-2013
On Wed, 2013-10-02, Eric Sosman wrote:
....
> One way to access the members is to write `instance.one.this'
> and `instance.three.that' and so on. Another is to aim a
> pointer variable at the particular inner struct you care about
> at the moment, and use that instead:
>
> struct inner *p = &instance.two;
> p->this = 42;
> p->that = 17;
>
> This technique can collapse additional levels if desired:
>
> struct atomic {
> double trouble;
> short snort;
> };
>
> struct inner {
> int this;
> int that;
> struct atomic redThing;
> struct atomic blueThing;
> };
>
> struct outer {
> struct inner one;
> struct inner two;
> struct inner three;
> } instance;
>
> struct atomic *q = &instance.three.redThing;
> q->trouble = 3.17;
> q->snort = 0;
>
> ... instead of `instance.three.redThing.trouble = ...' and so on.


A personal note:

I really prefer when people do this. Avoiding temporary pointers can
rather quickly lead to decreased readability, when the same (or
deceptively similar) long expression is repeated over and over again.

fie.foo[bar].baz.bat.barney = something;
fie.foo[bar].baz.bat.fred = something;
...

In C++ you can use references for this, but a constant pointer in C is
almost as good.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-03-2013
(E-Mail Removed) wrote:
> The Pascal "WITH" statement is one of those things that seemed a good
> idea at the time, but produces highly unreadable code.


A good analogy for posting via that awful google Usenet interface!

--
Ian Collins
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      10-03-2013
(E-Mail Removed) wrote:

> The Pascal "WITH" statement is one of those things that seemed
> a good idea at the time, but produces highly unreadable code.
> The problem is that any identifier within the "WITH" statement
> could now have a different meaning. So if you use a local
> variable or global variable within the WITH statement, and
> then rename or add or remove a member of the struct, your
> code might continue to compile with totally changed meaning.


I wonder how it compares to partial qualification in PL/I.
PL/I allows (as far as I know, inherited from COBOL) structure
members to be references with partial qualification if it isn't
ambiguous.

If you have A.B.C, and there are no other C's in the program,
then C is enough. If there are, either A.C or B.C might work.

PL/I also allows moving the subscripts in a structure reference.
A(I).B(J).C(K) can be written A.B.C(I,J,K) or vice versa.
(The compiler knows where they go in any case.) This one I
am more sure came from COBOL. As I understand it, COBOL only
allows for 1D arrays, so structure nesting is used to generate
higher dimensionality.

-- glen
 
Reply With Quote
 
Fred K
Guest
Posts: n/a
 
      10-04-2013
On Thursday, October 3, 2013 12:21:27 PM UTC-7, glen herrmannsfeldt wrote:
> (E-Mail Removed) wrote:
>
>
>
> > The Pascal "WITH" statement is one of those things that seemed

>
> > a good idea at the time, but produces highly unreadable code.

>
> > The problem is that any identifier within the "WITH" statement

>
> > could now have a different meaning. So if you use a local

>
> > variable or global variable within the WITH statement, and

>
> > then rename or add or remove a member of the struct, your

>
> > code might continue to compile with totally changed meaning.

>
>
>
> I wonder how it compares to partial qualification in PL/I.
>
> PL/I allows (as far as I know, inherited from COBOL) structure
>
> members to be references with partial qualification if it isn't
>
> ambiguous.
>
>
>
> If you have A.B.C, and there are no other C's in the program,
>
> then C is enough.


What a terrible idea!
Suppose I have a struct AA containing members a through z, each of which is itself a different kind of struct of, and those structs contained members aa through zz, etc., all of these being defined in different files.

Then my code says
int i = www;

It would be a nightmare trying to find the struct that this instance of www belongs to, especially if some of those modules contained local variables named www.


<snip>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Software must support the user's settings for font size but how itworks with swing? Markus Lemcke Java 6 05-01-2013 05:16 PM
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
Does Python have equivalent to MATLAB "varargin", "varargout", "nargin", "nargout"? openopt@ukr.net Python 8 11-17-2011 02:22 PM
HELP: C/C++ equivalent of dos,crt Pascal units dhruba.bandopadhyay@hotmail.com C Programming 6 09-02-2006 11:31 AM
HELP: C/C++ equivalent of dos,crt Pascal units dhruba.bandopadhyay@hotmail.com C++ 5 09-02-2006 11:31 AM



Advertisments