Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: golem on November 01, 2003, 03:51:01 PM

Title: OpenDevice (C help needed please!)
Post by: golem on November 01, 2003, 03:51:01 PM
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!

Thanks
Title: Re: OpenDevice (C help needed please!)
Post by: chris on November 01, 2003, 05:40:27 PM
It tells the compiler what the following variable type is.

Your example:
(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?  :-)

Chris
Title: Re: OpenDevice (C help needed please!)
Post by: golem on November 01, 2003, 07:05:07 PM
Yes I keep reading your reply and its starting to make sense!

Thanks Chris
Title: Re: OpenDevice (C help needed please!)
Post by: iamaboringperson on November 01, 2003, 11:28:47 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.

Quote
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).
Title: Re: OpenDevice (C help needed please!)
Post by: Cymric on November 02, 2003, 10:19:00 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.
Title: Re: OpenDevice (C help needed please!)
Post by: golem on November 02, 2003, 11:38:39 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!