Xref: utzoo alt.msdos.programmer:607 comp.sys.ibm.pc:37305 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!gem.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!bloom-beacon!eru!luth!sunic!sics.se!sics.se!levitte From: levitte@garbo.bion.kth.se (Tommy Levitte) Newsgroups: alt.msdos.programmer,comp.sys.ibm.pc Subject: Re: Turbo C far pointers Message-ID: Date: 31 Oct 89 11:42:45 GMT References: <565@titan.tsd.arlut.utexas.edu> <990@becker.UUCP> Sender: news@sics.se Organization: Royal Institute of Technology, Stockholm Lines: 63 In-Reply-To: bdb@becker.UUCP's message of 29 Oct 89 01:42:07 GMT In article <990@becker.UUCP> bdb@becker.UUCP (Bruce Becker) writes: Bruce> Xref: sics.se alt.msdos.programmer:153 comp.sys.ibm.pc:29919 Bruce> In article <565@titan.tsd.arlut.utexas.edu> elrond@titan.tsd.arlut.utexas.edu (Brad Hlista) writes: Bruce> |far *ptr; Bruce> |main() [... unimportant things deleted ...] Bruce> | f(i=0;i<1000;i++) Bruce> | *(ptr+i)=3.1415927; Bruce> Should be: *((float *)(ptr+i)) = 3.1415927; (* GAACK *) This means you should use the i'th INTEGER location, not float! Wow. The effect when printing out would be quite a sight... No, the correct syntax would be : *((float *)ptr + i) This way, you tell the compiler that ptr really is a float pointer, then offset the whole thing with i. In your solution, the offset would be 2*i bytes (2 = sizeof(int)). In mine, the offset would be 4*i bytes (4=sizeof(float)) !!!! Bruce> | for(i=0;i<10;i++) Bruce> | printf(" %f", (float) *(ptr+i) ) ; /* is yielding 3.00000 */ Bruce> Should be printf(" %f", *((float *)(ptr+i)) ) ; /* is yielding 3.1415927 */ Likewise here. Use *((float)ptr + i) instead ! Bruce> sizeof(far) == sizeof(float); is there a Bruce> "far long" type? Better yet, a "far float"? Wow. sizeof(far) ! Never seen that before. This would mean 'far int', which is not (as far as I know) permitted in TC. far is only applied on pointer and functions, to tell the compiler you need a 4-byte adress to get those objects. Thus, far is a modifier, not a type. Now, about int. int is the same as short in TC, a 2-byte integer. float is a 4-byte float. One IMPORTANT rule when you program in C: NEVER assume anything about the size of differnet types. NEVER !!!!! Bruce> I didn't actually try this so if your compiler Bruce> barfs on "*((float *)ptr)" where "ptr" is a Bruce> "far *" declaration, well... you could try Bruce> "far float * ptr;", etc... Quite surprising. I just tried it with Turbo C v 2.0, writing *((float *)ptr+i), and it worked just nice. writing *((float *)(ptr+i)) gave me 0.000000 when I tried it... -- Tommy Levitte gizmo@nada.kth.se gizmo@kicki.stacken.kth.se gizmo@ttt.kth.se