Welcome, Guest. Please login or register.

Author Topic: OpenDevice (C help needed please!)  (Read 2187 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline golemTopic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 432
    • Show only replies by golem
OpenDevice (C help needed please!)
« 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
                                                             
A1200 desktop, Blizzard 1260, OS3.9BB2, Indivision Mk II, SCSI Jaz, Ethernet
A1200 desktop, Blizzard 1230, OS3.1, Ethernet
A500, OS1.3
 

Offline chris

Re: OpenDevice (C help needed please!)
« Reply #1 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
"Miracles we do at once, the impossible takes a little longer" - AJS on Hyperion
Avatar picture is Tabitha by Eric W Schwartz
 

Offline golemTopic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 432
    • Show only replies by golem
Re: OpenDevice (C help needed please!)
« Reply #2 on: November 01, 2003, 07:05:07 PM »
Yes I keep reading your reply and its starting to make sense!

Thanks Chris
                                                             
A1200 desktop, Blizzard 1260, OS3.9BB2, Indivision Mk II, SCSI Jaz, Ethernet
A1200 desktop, Blizzard 1230, OS3.1, Ethernet
A500, OS1.3
 

Offline iamaboringperson

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 5744
    • Show only replies by iamaboringperson
Re: OpenDevice (C help needed please!)
« Reply #3 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).
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: OpenDevice (C help needed please!)
« Reply #4 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.
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline golemTopic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 432
    • Show only replies by golem
Re: OpenDevice (C help needed please!)
« Reply #5 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!
 
                                                             
A1200 desktop, Blizzard 1260, OS3.9BB2, Indivision Mk II, SCSI Jaz, Ethernet
A1200 desktop, Blizzard 1230, OS3.1, Ethernet
A500, OS1.3