Hi All
I am trying to understand offset in C and create it to print out the members reside on memory location using offset.
I have written following code:
Code:
#include<stdio.h>
#include<conio.h>
typedef struct
{
char name[10];
int age;
char address[100];
} record;
int main()
{
record rec,*ptr,p;
int offset = 0;
memset(&rec,0x00,sizeof(rec));
memcpy(rec.name,"NICKMAN",7);
memcpy(rec.address,"13 ALMOND STREET",20);
rec.age = 17;
ptr = &rec;
offset = (char *)rec.name - (char *)ptr;
return 1;
}
Is this the correct way of creating offset?
Also, how can I print the members using it.
Can any body me further from here?
Thanks
Nickman