LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2011, 11:50 AM   #1
deadpixel01
LQ Newbie
 
Registered: Jul 2011
Location: UK
Posts: 3

Rep: Reputation: Disabled
Can someone give me a hand with pointers in C?


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 if;
    
FILE *fp;

    
fp fopen("records.dat","w+");
    for(
i=0i<NRECORDSi++) {
        
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.  */

    
open("records.dat",O_RDWR);
    
mapped = (RECORD *)mmap(0NRECORDS*sizeof(record), 
                          
PROT_READ|PROT_WRITEMAP_SHAREDf0);

    
mapped[43].integer 243;
    
sprintf(mapped[43].string,"RECORD-%d",mapped[43].integer);

    
msync((void *)mappedNRECORDS*sizeof(record), MS_ASYNC);
    
munmap((void *)mappedNRECORDS*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.
 
Old 07-25-2011, 12:55 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
In "C" by definition

Code:
*(ptr + some_int)
is equivalent (in the sense of access to the same memory location) to

Code:
ptr[some_int]
.

And you have a pointer to an area of consecutive structs.
 
Old 07-25-2011, 01:33 PM   #3
deadpixel01
LQ Newbie
 
Registered: Jul 2011
Location: UK
Posts: 3

Original Poster
Rep: Reputation: Disabled
That cleared it up for me,

Cheers Sergie.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please give me a hand nesta1982 Linux - Networking 1 06-03-2009 10:41 AM
Can someone give me a hand? TemptingWheat Linux - Software 2 10-10-2004 07:37 AM
I'm not able to choose anymore between my OSes. Give me a hand please.. Santorres Linux - General 4 12-16-2003 08:50 PM
Can any PERL gurus give me a hand, please? WorldBuilder Programming 16 10-23-2003 05:08 PM
How to Install RPM's ! Please give me a hand rising_sun Linux - Newbie 4 06-04-2002 11:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration