歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux offsetof在用戶態C語言實現及示例

Linux offsetof在用戶態C語言實現及示例

日期:2017/3/1 10:51:27   编辑:Linux編程

Linux系統提供的offsetof方法是得到一個結構體中的一個成員字段的此結構體中的偏移字節,現在用戶態進行實現。

在用戶態進行實現的例子:得到一個結構體中的子結構體中的某一個字段的值

假設前題:只知道大結構的類型、子結構體的名字、子結體的某一字段的名字

思路:先得到子結體中的字段相對於大結構體的偏移量,然後再得到此字段的地址值,然後再得到此字段的類型,由此類型把得到的地址值進行強制轉換後就可以得到此字段的值了,代碼實現:


#include <stdio.h>

#include <stdlib.h>

//子結構體


typedef struct student{


int age;
int length;
char c;
long width;
char mm[4];
int nn;
}ST_STUDENT;

//大結構體


typedef struct school
{
int schNo;
ST_STUDENT first;
int sumpeple;
}ST_SCHOOL;

//示例為求得子結構體中的char c的值


int main()


{
int offset, ret, nowadd;
char *ch = malloc(sizeof(ST_SCHOOL));
strcpy(ch, "yygydjkthh");
printf("ch addr is 0x%x char str is %s\n", ch, ch);
ST_SCHOOL sccxzz;
sccxzz.first.c = 'a';
offset = (char *)(&(((ST_SCHOOL *)ch)->first.c)) - ch;
nowadd = (char *)((char *)&sccxzz + offset);
typeof(sccxzz.first.c) m = *((typeof(sccxzz.first.c) *)nowadd);
printf("offset is %d m is %c m len is %d\n", offset, m, sizeof(m));
printf("ch str is %s\n", ch);
free(ch);
exit(0);
}


其中:

//下行得到char c字段相對於大結構體的偏移量


offset = (char *)(&(((ST_SCHOOL *)ch)->first.c)) - ch;


//下行為得到char c的地址


nowadd = (char *)((char *)&sccxzz + offset);


//下行為得到求得的char c地址的值並保存到以char c字段類型聲明的一個變量中

//其中typeof為得到某一變量的類型,下行也即為: char m = *((char *)nowadd);


typeof(sccxzz.first.c) m = *((typeof(sccxzz.first.c) *)nowadd);


運行結果為:

ch addr is 0x9191008 char str is yygydjkthh
first.c is a
sccxzz add is 0xbfda205c nowadd is 0xbfda2068 first.c add is 0xbfda2068
offset is 12 m is a m len is 1
ch str is yygydjkthh

Copyright © Linux教程網 All Rights Reserved