Path: utzoo!attcan!uunet!husc6!mailrus!chianti!shane From: shane@chianti.cc.umich.edu (Shane Looker) Newsgroups: comp.sys.mac.programmer Subject: Re: LSC 3.0 "Context" + Passing parameters Message-ID: <586@mailrus.cc.umich.edu> Date: 25 Jul 88 18:24:46 GMT References: <3653@bnrmtv.UUCP> Sender: usenet@mailrus.cc.umich.edu Reply-To: shane@um.cc.umich.edu (Shane Looker) Distribution: na Organization: University of Michigan Computing Center, Ann Arbor Lines: 91 In article <3653@bnrmtv.UUCP> west@bnrmtv.UUCP (andrew west) writes: !----------------------------------------------------------------- ! !2) This may not be a LSC question but I encountered the problem when ! I was using LSC so what the heck... ! ! Some sample code: ! ! ------------------------------------------------------------------ ! ! #include "stdio.h" ! #define MY_FIRST_VARIABLE 16 ! #define ANOTHER_ONE 0 ! #define YET_ANOTHER_ONE 256 ! ! #define int32 long int /* Change to correct type to create 32, 16 */ ! #define int16 int /* and 8 bit variables on your machine */ ! #define int8 char ! ! ! int16 data[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ! ! !int my_function (my_first_var, another_one, my_data, yet_another); ! ! int32 my_first_var; ! int32 another_one; ! int16 *my_data; ! int32 yet_another; ! ! { ! printf("yet_another = %d\n", yet_another); ! /* Or you can use the fancy new source level debugger */ ! ! ! } /* end of "my_function" */ ! ! ! main() ! ! { ! ! my_function (MY_FIRST_VARIABLE, ANOTHER_ONE, ! data, YET_ANOTHER_ONE); ^^^^ Danger, Will Robinson, Danger, Danger. Be careful with something like this. LSC will pass structures on the stack. You should pass &data to make sure you are passing what you want. ! ! } ! ! ! -------------------------------------------------------------- ! ! When I tried running this, the values the variables contained inside ! the function were not the sames as the values I (thought I) passed ! in from outside the function. Passing in a 16 would yield a 11 ! inside the function for example. ! ! After I played around for a while, I finally discovered that the ! problem was related to the values I was passing into the ! function--in order to have my "defined" variables interpreted ! correctly, I had to cast each one of them to the same type as the ! types inside the function. This certainly makes sense if the ! variables outside the function were a different type, but it seemed ! strange at first since I was using "define"s, not "real" variables. ! I guess the compiler had to put *something* on the stack. ! ! my_function ( (int32) MY_FIRST_VARIABLE, (int32) ANOTHER_ONE, ! (int16) data, (int32) YET_ANOTHER_ONE); ! ! ! After all that, my question is: Does LSC (or do other C compilers ! in general) default to a particular type (int, for example) in a ! case like this where the variables passed into a function are ! "declared" using define statements? ! ! Andrew West Your problems may be related to passing a structure on the stack (not sure) or they may be related to the fact that a compiler will usually assume that you are passing an (int) if it fits. (int) varies from machine to machine and compiler to compiler. I would strongly advise using prototypes. They are very handy, since they can not only save you on problems like this, but they allow the compiler to know what you want to pass, not second guess you. Shane Looker Looker@um.cc.umich.edu