Afficher un message
Vieux 17/10/2007, 13h32   #1
Jim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Pointers to structs - !

Ok, I'm having 'fun' with pointers to structures.

I've got some code that looks something like this:

====================
typedef struct
{
unsigned long *clno, *lastHistoryRecord;
} aRecord;

....
....

void main()
{
....
....
theRecord aRecord;

theRecord.clno = (unsigned long*) malloc(sizeof(long));
theRecord.lastHistoryRecord = (unsigned long*) malloc(sizeof(long));

getArecord( infile, &theRecord, recordNumber );

printf( "%ld %ld\n", theRecord.clno, theRecord.lastHistoryRecord );
....
....
}


int getArecord( FILE *infile, aRecord *thisRecord, long recordNumber )
{
....
....
fread( &thisRecord->lastHistoryRecord, 1L, sizeof(long), infile );
fread( &thisRecord->clno, 1L, sizeof( long ) * 1, infile );
....
....
return result;
}

====================

(large chunks of code snipped for brevity)


Now, it _seems_ to do as I expect. Let's say that I run this and get:

1000 1234

as a result. If I change the printf line to say:

printf( "%ld %ld\n", theRecord.clno-1, theRecord.lastHistoryRecord );

then I get

996 1234. I would have expected 999. So I'm obviously doing something very,
very stupid somewhere[0]. Could someone enlighten me? FWIW I've also tried
--theRecord.clno and (theRecord.clno)-1 but they both do the same.

Many thanks. Sorry if this is a really, really obvious one..!

Jim
[0] apart from not checking the mallocs for errors
--
http://www.ursaMinorBeta.co.uk
BANTEER (n. archaic)
A lusty and raucous old ballad sung after a particularly spectacular araglin
(q.v.) has been pulled off.
  Réponse avec citation
 
Page generated in 0,05789 seconds with 9 queries