Like Tree3Likes
  • 2 Post By stahta01
  • 1 Post By CommonTater

With Statement Pascal to C

This is a discussion on With Statement Pascal to C within the C Programming forums, part of the General Programming Boards category; I am translating a program atm from pascal to c, but one statement i am facing is with. Code: with ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    15

    With Statement Pascal to C

    I am translating a program atm from pascal to c, but one statement i am facing is with.

    Code:
    with typtab[ttptr] do
    begin
         ...
    end
    Now my question is how can i represent this in c, typtab is a structure and ttptr is a pointer as as its also an array. Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Now my question is how can i represent this in c,
    You can't.

    C has no equivalent for the with statement.

    You will need to use struct notation as...
    Code:
    mystruct[idx].variable;
    
    // or 
    
    mystruct[idx]->variable;
    On an item by item basis.

    Also note that C has no sets, ranges or strings either.

    (And there you have 4 reasons why I miss good ole Pascal every day.)
    Last edited by CommonTater; 12-08-2011 at 11:58 AM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    15
    Thanks for the quick reply, and i dont think pascal is old, my teacher only use that
    and apl2. But anyway how can i translate the following in c:




    Code:
        typtab: array[ 0 .. ttsize](*type table*)
     of typrec;
            typrec = record
    
    
                     size: integer;
    
    
                     case form: typform of scalarfrm: ();
    
    
                     subrangfrm: ( rangtyp: typtabptr;
     min,
     max: constrec);
             enumfrm: ( maxenum: 0 .. 255);
    
    
    
    
        with typtab[ttptr] do
        begin
             ...
        end


    Thanks if you need more question about some variable just reply, i appreciate the help. if it can be done.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,683
    Quote Originally Posted by Jseb View Post
    Code:
             ...
    The stuff that needs fixed is the ...; and you think it is not important to post it? Why?

    Tim S.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,683
    Code:
    with typtab[ttptr] do
    begin
         ...
    end
    change to

    Code:
         ...
    With fixes as needed normally adding "typtab[ttptr]." or "typtab[ttptr]->" prefix.

    Tim S.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jseb View Post
    Thanks for the quick reply, and i dont think pascal is old, my teacher only use that
    and apl2. But anyway how can i translate the following in c:

    Code:
        typtab: array[ 0 .. ttsize](*type table*)
     of typrec;
            typrec = record
    
    
                     size: integer;
    
    
                     case form: typform of scalarfrm: ();
    
    
                     subrangfrm: ( rangtyp: typtabptr;
     min,
     max: constrec);
             enumfrm: ( maxenum: 0 .. 255);
    
    
    
    
        with typtab[ttptr] do
        begin
             ...
        end


    Thanks if you need more question about some variable just reply, i appreciate the help. if it can be done.
    Tell you what... it's been quite a while since I did Pascal, nearly 10 years...
    I think your best bet would be to grab a C tutorial off the web and work with that... It's not that I don't want to help, I'm just not sure I'd be giving you all the right answers.

    Again there are no ranges in C ... all array indeces start at 0 and go to size-1. If you need non-0 based notations you'll have to use offsets.

    Also forget any notion of OOP... no classes, no forms, no objects...

    Because of the gramattical structure you have to define the struct (record) first,
    then you can make an array of them. The most straightforward way is probably like this...

    Code:
    // prototype
    struct t_structname
      { int variable;
         char text[size];
         int *pointer; };
    
    // declare an array
    struct t_structname data[elements];
    I'm sorry if that's a little cryptic but it should give you the idea.
    Last edited by CommonTater; 12-08-2011 at 01:29 PM.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    15
    Sorry for wait, thanks for the reply I would say the missing part would be this

    Code:
    with typtab[ ttptr] do
                begin
                  size := 1;
                  form := scalarfrm
                end;

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You might find it easier to move away from the pascal code, and move into whatever the logic is actually doing. C can work from that point, although probably not as eloquently as pascal. Trying to "convert" pascal code to C, is like trying to make a fashion model by putting clothes and makeup, on a horse.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,683
    Quote Originally Posted by Jseb View Post
    Sorry for wait, thanks for the reply I would say the missing part would be this

    Code:
    with typtab[ ttptr] do
                begin
                  size := 1;
                  form := scalarfrm
                end;

    Code:
                 
        typtab[ ttptr + CONST_TO_FIX_ARRAY_START].size = 1;
        typtab[ ttptr + CONST_TO_FIX_ARRAY_START].form = scalarfrm;
    Tim S.
    CommonTater and Jseb like this.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Any reason why you're going to C and not C++?
    In C++ the approximate equivalent of a 'with' statement would typically be a constructor, or at least some other member function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    Any reason why you're going to C and not C++?
    In C++ the approximate equivalent of a 'with' statement would typically be a constructor, or at least some other member function.
    In Pascal with is a shorthand tool... with structname do ... maskes off the struct.variable syntax so you can just use variable = whatever. It's not part of OOP really, it's just a programmer's convenience to avoid typing complicated variable names.

    Code:
    with typtab[ ttptr] do
                begin
                  size := 1;
                  form := scalarfrm
                end;
    
    in Pascal is the same as...
    
    tytab[ttptr].size = 1;
    tytab[ttptr].form = scalarfrm;
    
    ... in C.
    For small examples like the above there's no benefit, but when you get a record with 30 or 40 variables in it, it helps avoid a lot of errors.
    Jseb likes this.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    15
    I am making a compiler for a class, teacher is old school, i love c++, would of been done the project long time ago, but must be done in c. I appreciate the help, it was the way i was going just wanted to make sure thanks a lot . And yeah the original codes as like 3000 line of code, some of the record do have some variant i dont think 40 but closed but thanks

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Jseb View Post
    Thanks for the quick reply, and i dont think pascal is old
    What you think of a fact doesn't make it any less of a fact. But I suppose old is subjective. For example, if you were Pascal, then Pascal wouldn't be old at all. But since you are you, the latter is actually old.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by CommonTater View Post
    In Pascal with is a shorthand tool... with structname do ... maskes off the struct.variable syntax so you can just use variable = whatever. It's not part of OOP really, it's just a programmer's convenience to avoid typing complicated variable names.
    I know all about Pascal and the with statement. I programmed in Pascal for many years before programming in C.

    Sure, it's arguably not part of OOP in Pascal, but my point was that if you translated it to C++, then one would most likely use OOP and turn it into either a constructor call or a method call.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    I know all about Pascal and the with statement. I programmed in Pascal for many years before programming in C.
    Me too... almost the entire lifetime of the language in fact. (Right up till Borland killed it with Delphi)

    Sure, it's arguably not part of OOP in Pascal, but my point was that if you translated it to C++, then one would most likely use OOP and turn it into either a constructor call or a method call.
    Not being very proficient in C++ I'll take your word for it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pascal
    By cogeek in forum Tech Board
    Replies: 32
    Last Post: 11-20-2004, 03:19 AM
  2. C or PASCAL ?
    By AProg in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 05-22-2003, 01:15 PM
  3. pascal
    By sentienttoaster in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 12-06-2002, 02:41 AM
  4. some one here know pascal??
    By GodLike in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-03-2002, 02:37 AM
  5. C/C++ Pascal?
    By KrAzY CrAb in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21