On 26-Nov-1999, Rob Walker <[EMAIL PROTECTED]> wrote:
> 
> I am not much of a C programmer, which is something you will all learn
> soon enough if you haven't figured it out already.  :-)
> 
> I need a hand understanding something.
> 
> in splitreg.c I see a line which reads
> 
> HDR (DATE)
> 
> and up at the beginning of the file, I see these lines:
> 
> #define HDR(NAME)                                               \
> {                                                               \
>    BasicCell *hcell;                                            \
>    hcell = xaccMallocTextCell();                                \
>    reg->header_label_cells[NAME##_CELL] = hcell;                \
> }
> 
> now, what does that do?  I thought that I understood what a #DEFINE
> did, but I guess not.  Aren't they kinda like search and replaces for
> the preprocessor?  I thought that they were.  But now this blows that
> theory out of the water.  
> 
> I continue with my thoughts.  NAME is the argument which gets passed
> into the function.  I get that.  hrmm...  BasicCell is the type of the
> variable *hcell.  I don't get the *, but owell.  Does that mean that
> this is really a pointer to the variable hcell, which really exists
> outside of this function?  _this_ function?  _which_ function?

hcell is a pointer to a BasicCell.  xaccMallocTextCell creates this cell
and assigns it to hcell (probably a nice new one, since malloc is the C
function call that allocates memory, so xaccMallocTextCell probably
calls malloc).

> xaccInitSplitRegister ???  I don't see a hcell in
> xaccInitSplitRegister.  so hcell gets the return value of
> xaccMallocTextCell... and that is...  in src/register/textcell.c, line
> 52, okay, it returns cell.  does that mean that cell is the name of
> the cell which just got initialized?  

hcell is the name of the variable that just got initialized.  It points
to the cell that was allocated but might not be initialized yet.
In this case I suspect it is initialized in the xaccMallocTextCell 
function.


> 
> okay, so hcell is a cell (or the name of the cell?) which is getting
> initialized.  that's cool.  for instance, in one point, hcell could be
> the first header cell.  hrmm...  
> 
> ok, NAME shows up on that last line, and it ends up being
> reg->header_label_cells[DATE##_CELL] = hcell; right?  now, I _really_
> don't have a clue with "all that arrow stuff".  what does that last
> line do?

## joins two things together, so you end up with

reg->header_label_cells[DATE_CELL] = hcell;

The data structure reg, which I assume is something that points at the
register data structure, has a field inside it call header_label_cells.
This is an array, and we go to a particular element of it and assign
that element the value hcell.

-> means the same as * followed by .

So -> is the same as de-referencing (following) the pointer (that's what
* does) and then going to a particular field (.)

Note that * is used in C both as an operator to do dereferencing and
as a pointer type.  This is one of the many joys of C.

[x] goes to the xth cell in the array preceding it.  DATE_CELL is
probably defined to be some number elsewhere in the code.


> hey!  is #define different than #DEFINE ???  is _that_ what I am
> missing?  

No, you just didn't know what -> did.

> 
> rob
> 
> ps.  if i understand #define's, then every time we see HDR (DATE),
>      those three lines get copied into the source file each time on
>      the way to/through the preprocessor.  Doesn't that bring out a
>      ton extra BasicCell *hcell; lines??

Maybe, but it doesn't really matter, the variable is inside a very small
scope (the curly braces) and so it isn't going to cause any trouble.

-- 
The quantum sort: 
        while (!sorted) { do_nothing(); }
Tyson Dowd   <[EMAIL PROTECTED]>   http://tyse.net/

--
Gnucash Developer's List 
To unsubscribe send empty email to: [EMAIL PROTECTED]

Reply via email to