OK, the following code compiles and works perfectly, however there is something I don't fully understand;
PHP Code:
/* We start by defining a RECORD structure
and then create NRECORDS versions each recording their number.
These are appended to the file records.dat. */
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
typedef struct {
int integer;
char string[24];
} RECORD;
#define NRECORDS (100)
int main()
{
RECORD record, *mapped;
int i, f;
FILE *fp;
fp = fopen("records.dat","w+");
for(i=0; i<NRECORDS; i++) {
record.integer = i;
sprintf(record.string,"RECORD-%d",i);
fwrite(&record,sizeof(record),1,fp);
}
fclose(fp);
/* We now change the integer value of record 43 to 143
and write this to the 43rd record's string. */
fp = fopen("records.dat","r+");
fseek(fp,43*sizeof(record),SEEK_SET);
fread(&record,sizeof(record),1,fp);
record.integer = 143;
sprintf(record.string,"RECORD-%d",record.integer);
fseek(fp,43*sizeof(record),SEEK_SET);
fwrite(&record,sizeof(record),1,fp);
fclose(fp);
/* We now map the records into memory
and access the 43rd record in order to change the integer to 243
(and update the record string), again using memory mapping. */
f = open("records.dat",O_RDWR);
mapped = (RECORD *)mmap(0, NRECORDS*sizeof(record),
PROT_READ|PROT_WRITE, MAP_SHARED, f, 0);
mapped[43].integer = 243;
sprintf(mapped[43].string,"RECORD-%d",mapped[43].integer);
msync((void *)mapped, NRECORDS*sizeof(record), MS_ASYNC);
munmap((void *)mapped, NRECORDS*sizeof(record));
close(f);
exit(0);
}
When the struct RECORD pointer *mapped is being accessed, it's accessed as an ordinary variable
e.g mapped[43].integer = 243;
and not mapped[43]
->integer = 243.
I was always tough that if you needed to access a element of a struct that was accessed via a pointer, you had to use -> instead of . , clearly I've misunderstood something, could someone give me a brief explication on this matter?
Cheers.