Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get/set a struct member by offset

Tags:

c

struct

Ignoring padding/alignment issues and given the following struct, what is best way to get and set the value of member_b without using the member name.

struct mystruct {     int member_a;     int member_b; } struct mystruct *s = malloc(sizeof(struct mystruct)); 

Put another way; How would you express the following in terms of pointers/offsets:

s->member_b = 3; printf("%i",s->member_b); 

My guess is to

  • calculate the offset by finding the sizeof the member_a (int)
  • cast the struct to a single word pointer type (char?)
  • create an int pointer and set the address (to *charpointer + offset?)
  • use my int pointer to set the memory contents

but I get a bit confused about casting to a char type or if something like memset is more apropriate or if generally i'm aproching this totally wrong.

Cheers for any help

like image 445
Chris Farmiloe Avatar asked Jan 11 '10 18:01

Chris Farmiloe


People also ask

How do you find the offset of a structure member?

Use offsetof() to find the offset from the start of z or from the start of x . #include <stddef. h> size_t offsetof(type, member); offsetof() returns the offset of the field member from the start of the structure type.

Can a struct have itself as a member?

A structure T cannot contain itself.


Video Answer


2 Answers

The approach you've outlined is roughly correct, although you should use offsetof instead of attempting to figure out the offset on your own. I'm not sure why you mention memset -- it sets the contents of a block to a specified value, which seems quite unrelated to the question at hand.

Here's some code to demonstrate how it works:

#include <stdio.h> #include <stdlib.h> #include <stddef.h>  typedef struct x {     int member_a;     int member_b; } x;  int main() {      x *s = malloc(sizeof(x));     char *base;     size_t offset;     int *b;      // initialize both members to known values     s->member_a = 1;     s->member_b = 2;      // get base address     base = (char *)s;      // and the offset to member_b     offset = offsetof(x, member_b);      // Compute address of member_b     b = (int *)(base+offset);      // write to member_b via our pointer     *b = 10;      // print out via name, to show it was changed to new value.     printf("%d\n", s->member_b);     return 0; } 
like image 73
Jerry Coffin Avatar answered Sep 28 '22 04:09

Jerry Coffin


The full technique:

  1. Get the offset using offsetof:

    b_offset = offsetof(struct mystruct, member_b);

  2. Get the address of your structure as a char * pointer.

    char *sc = (char *)s;

  3. Add the add the offset to the structure address, cast the value to a pointer to the appropriate type and dereference:

    *(int *)(sc + b_offset)

like image 25
R Samuel Klatchko Avatar answered Sep 28 '22 02:09

R Samuel Klatchko