C question

From: Eric J. Korpela <korpela_at_ssl.berkeley.edu>
Date: Thu Aug 1 11:35:03 2002

> // Read ROM directly to memory. This works.
> ppmem = &ucMem[roms[i].iROMStart + MEM_ROM] ;
> fread(ppmem, sizeof(byte), roms[i].iROMLen, pFH) ;
>
> //this doesn't work...
> //fread(&ucMem[roms[i].iROMStart + MEM_ROM], sizeof(byte), roms[i].iROMLen,
> pFH) ;
>
> //...nor does this
> //fread((char *)ucMem[roms[i].iROMStart + MEM_ROM], sizeof(byte),
> roms[i].iROMLen, pFH) ;
>
> So here's where I'm missing it. Under all three fread scenarios, the
> compiler doesn't throw a warning...they all compile cleanly. But only the
> first works.

Lack of warnings or code don't have anything to do with whether the code
is correct...

The last one is the easiest to figure out....

Call fread with the following parameters:
  1. byte value at address (ucMem+roms[i].iROMStart+MEM_ROM) converted into
      a pointer to type char.
  2. The value 1 (assuming byte is typedefed to "unsigned char")
  3. The value stored in roms[i].iROMLen
  4. The value stored in pFH.

It's fairly easy to see why the first parameter is wrong... No warning is
issued because the type of the first parameter is correct. Without the
typecast, a warning should be issued.

The previous two look equivalent to me. I don't really see a significant
difference. Are you sure "i" and "roms" are set to what you think it should be.

Personally, I would have written the first parameter as
"ucMem+roms[i].iROMStart+MEM_ROM" as I think it more clearly represents
what you mean.

What is the error reported? What are the return values from fread? (You
should be checking that the return value equals roms[i].iROMLen.)

Eric
Received on Thu Aug 01 2002 - 11:35:03 BST

This archive was generated by hypermail 2.3.0 : Fri Oct 10 2014 - 23:34:35 BST