[fpc-pascal] pointer arithmetic help required

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Oct 15 14:54:17 CEST 2009


Hi,

Sorry, I suck at this pointers think. Currently I am reading one
record in correctly and from there is all corrupted data. :-(

Seeing that the first block of data (pEntry^ below) is correct, it
seems my problem is incrementing to the next block of data. Here are
the various variables I am working with

_pContentsData = a generic Pointer type that points to a block of data
(containing multiples of various structures). XXX bytes of
information.

pEntry = this is a pointer to a specific starting structure type and
will increment in blocks of data to point to the various structures
stored in _pContentsData. Each pEntry has a Length property that says
how many bytes of data is used for all it's information.

pEnd = the ending point of all the data held by _pContentsData. This
variable is just a safety net, so pEntry doesn't move past the end of
data block by accident.

I have the following code...

------------------------
  pEntry := _pContentsData;
     // below toclen is in bytes
  pEnd := _pContentsData + _pHeader^.toclen;

// START LOOP
  [(a)...do various things with data pointed to by pEntry..]
  ....

  // now increment pEntry to point to next data structure
  // below, .Length is in bytes
  inc( pEntry, pEntry^.Length);
  // now repeat from (a) above until all data is processed
// END LOOP
------------------------


Question:
Am I incrementing the pEnd and pEntry in the correct way? They are
pointers to structures, so I believe I don't need to dereference them,
because I am working with the pointer at that stage and not the actual
data.
Example:

  inc(pEntry, pEntry^.Length);
vs
  inc(pEntry, pEntry^.Length * sizeof(byte));


Luckily the above method is not the only way to read the data I am
interrested in. There is also a redundant array of file offsets
pointing to each pEntry I am interrested in. Using the array works
fine, but using the incrementing method shown below, I am having
problems. :-(
Purely for the purpose of learning I would like to be able to get both
methods working - how else am I supposed to learn these things. :)

[I think I need to dig up my old college handbooks and relearn the
basics of pointers and record structures with binary file data]

-- 
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the fpc-pascal mailing list