1826614 Members
2847 Online
109695 Solutions
New Discussion

With in C?

 
SOLVED
Go to solution
Greg White
Frequent Advisor

With in C?

Hi all:

I'm in the middle of my big Pascal to C port and I have a problem. My original Pascal records contain many deeply nested records and variants. In Pascal it is very easy to address these field using the WITH statement. WITH w,x,y,z DO
BEGIN some assignments END. In C, the only thing I can do is make very long assignments. Is there an easier way?

Thanks
I know how to do it in pascal.
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: With in C?

Hi Pascal:

As I'm sure you are aware by now, C has no equivalent to Pascal's WITH. However, Standard Pascal has no equivalent to C's address operator and therein lies the method. You basically setup a point to a struct within your struct and then make the assignments using the pointer. Since I know this makes no sense, I'll attach a snippet of code.

Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: With in C?

Hi again:

Method B is to take advantage of the preprocessor and define a macro. Note the syntax of the macro WITH_C and especially the "\"'s to continue the macro. You might want to compile this snippet like this, cc -P dummy.c; this will simply preprocess the .c to produce a .i file (dummy.i). Examine the .i file to see how the macro was expanded. Also note, that I undef the macro as soon as I'm done so that another and possibly completely different WITH_C macro can be defined elsewhere. If is it a very common macro, you might include it in your header files so that many files can use it.

Regards and Happy C'ing, Clay
If it ain't broke, I can fix that.
Greg White
Frequent Advisor

Re: With in C?

Thanks Clay.

I knew there had to be a way. Why did you declare your variables in a separate block? Couldn't I have I simply declared them at the top of the function?

I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: With in C?

You bet, you could and sometimes should declare the temporary pointers at the top of a function; however, I thought you were one of those Pascal type guys who like information hiding and limited scope and such. The safest and best practice is to declare these variables at the block level. One can them reuse the same symbol over and over and the usual scoping rules take effect.

In fact, I generally use block scope variables
in most cases and especially for counters and indices.

Regards, Clay
If it ain't broke, I can fix that.
Greg White
Frequent Advisor

Re: With in C?

Thanks again.

One more question. In your example, if I have to assign values to both the b_recs and the c_recs within the a_rec, can I assign two pointers? Like this?

b_rec *bp = &(a.b);
c_rec *cp = &(bp->c);

bp->bi = 9;
cp->ci = 1;

Would that work?

I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: With in C?

Absolutely! By George, I think he's got it.

Once again, that normally should be with curly braces to make this stuff block scope. Also, the is no need to make long descriptive variables names like 'tmp_brec_pointer' or some such. The whole point is to make this shorthand - 'bp' is just fine. This is another reason to make the declarations block scope. They are always very near to the assignments and their meaning is then obvious and the 'disappear' when they have done their duty.

By the way, if you think the Pascal to C transition is fun, you will love the C to C++ transition. The learning curve is at least as steep.

Regards, Clay
If it ain't broke, I can fix that.