[comp.lang.c] Pointers to Constants

rothen@unix.cis.pitt.edu (Seth B Rothenberg) (01/01/90)

I will soon be converting a code generator from generating Fortran to
generating C.  One difference between the languages that looks like it
will give me some problems is argument passing.  Our programs
currently take advantage of the fact that fortran uses
call-by-address.  C does not do this, so I have to work out a
call-by-address algorithm.  (We cannot manually alter each program,
since we have over 500 programs in a pre-parsed format, where all
symbols are in a symbol table, including names of called subroutines,
names and types of variables and even punctuation marks.)


I originally thought

 
    (1) CALL FUNCT1(X,Y) --->    funct1(&x, &y);
    (2) CALL FUNCT1(ARRAY1) -->  funct1(array1);
    (3) CALL FUNCT1(PTR[2]) ---> funct1(&ptr[2]);

but (1) fails if X or Y are constants.  So I thought

        CALL FUNCT(1, 4) ---> funct1(&const[0], &const[1]); 

where const is an external array found in a separate file:

        int const[]={1, 4,....}


This, however, fails if there in the case of an expression such as:

        CALL FUNCT1( X + 5, Y + 2)

The only possible thing I thought of is

        CALL FUNCT1( X + 5, Y + 2) ---> {t[0]=x+5; t[1]=y+2; funct1(t, t+1) }

                                where t is declared   int t[10];

But this is ugly.  (Of course, the called subroutine can change t and
the change will be ignored.)

It is possible for the translator to examine the called function and
decide if call-by-address is needed (and whether it is a problem as in
this last case), but this is hard, and most of the parameters are
declared as needing call-by-address.

Does anyone have any ideas/comments on this problem?


Thanks

Seth Rothenberg
Information Systems
Western Psychiatric Institute and Clinic
rothen@unix.cis.pitt.edu

henry@utzoo.uucp (Henry Spencer) (01/01/90)

In article <21337@unix.cis.pitt.edu> rothen@unix.cis.pittsburgh.edu (Seth B Rothenberg) writes:
>The only possible thing I thought of is
>        CALL FUNCT1( X + 5, Y + 2) ---> {t[0]=x+5; t[1]=y+2; funct1(t, t+1) }
>                                where t is declared   int t[10];
>But this is ugly...

This really is about what you have to do.  (Don't forget to suppress the
use of the temporary when the thing being passed is addressable.)  Fortran
does this for you invisibly; C doesn't.
-- 
1972: Saturn V #15 flight-ready|     Henry Spencer at U of Toronto Zoology
1989: birds nesting in engines | uunet!attcan!utzoo!henry henry@zoo.toronto.edu