View Full Version : OpenDevice (C help needed please!)
11-01-2003, 08:51 AM
I'm learning how to program the serial device but finding it difficult to fathom the RKM examples.
e.g. return = OpenDevice(serial.device,0,(struct IoRequest *)IORequest, flags)
What I don't understand about that is what the (struct IORequest *) part does.
I understand you pass a pointer to an IORequest structure to the OpenDevice call but the above C declaration I no understand! I'm much more comfortable with 68k asm but figure it's time I learnt to do things properly!
11-01-2003, 10:40 AM
It tells the compiler what the following variable type is.
(struct IoRequest *)IORequest
Tells the compiler that the variable "IORequest" is a pointer to a struct IoRequest.
It has probably been declared earlier on with:
struct SerialIORequest *IORequest;
Or something like that. The serial.device needs a special IORequest structure, but OpenDevice defines struct IORequest as the type for the IORequest, so you need to tell it to use the variable as a type IORequest, rather than the defined type SerialIORequest.
Get it? :-)
11-01-2003, 12:05 PM
Yes I keep reading your reply and its starting to make sense!
11-01-2003, 04:28 PM
What you're talking about is called casting.
Casting is just a standard part of C. So you may want to study plain ANSI C for a while before getting onto reletively advanced stuff.
I'm much more comfortable with 68k asm but figure it's time I learnt to do things properly!You'l want to learn C if you're wanting to program for next generation machines(Pegasos).
11-02-2003, 03:19 AM
Basically what you're doing is telling the compiler to temporarily override its stong type checking. Normally, if you declare a variable in a function 'int', but then pass an 'unsigned long' to it, you'll get an error (or a warning at the very least) informing you there's a type mismatch. The problem here is that OpenDevice(), being a very general function to deal with any device, only accepts general IORequest structures. But in your program, you're dealing with SerialIORequests, AudioIORequests, SCSIIORequests, and what not. Passing such a structure to OpenDevice() would cause the compiler to flag an error, so you tell it: 'Look, right this moment, this SerialIORequest has type IORequest. Trust me, I know what I'm doing, so quit complaining.' You could even cast it to void *, or char * or somesuperblynewflashytype * if you wanted (and the function was defined with that type, otherwise you'll still get a type mismatch error).
As iamaboringperson said, you'd do well to start reading a book on ANSI-C. Get the (expensive but) definitive guide by Kernighan and Ritchie, 2nd edition. Worth every last penny.
11-02-2003, 04:38 AM
Thanks Cymric - I thought it had something to do with casting.
I have got Kernigan and Ritchie and keep meaning to read it cover to cover but I'm a book dipper and keep trying to fly before I can walk! Same goes for the full set of ROM Kernel Manuals I have at last got hold of.
I think I need to work through K & R compiling the examples as I go and bite the bullet!
vBulletin® v3.8.4, Copyright ©2000-2013, Jelsoft Enterprises Ltd.