Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointer to a member variable in a struct without dereferencing the member

6 views
Skip to first unread message

Roger

unread,
Nov 23, 2009, 2:33:36 PM11/23/09
to
All,

I have the below struct and I need to access the pointer to the "void
* data" member without actually dereferencing the member variable. I
could have gone with the approach of (ptr + sizeof(int) *4), but I
dislike this idea - if I change something in the structure, I would
have to revisit this part as well.

struct something {
int size;
int msglen;
int count;
int handle;
void *data;
};

struct something *ptr = (struct something *) msg;

char *data_ptr = ptr + sizeof(int) * 4;
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Keith Thompson

unread,
Nov 24, 2009, 4:56:57 AM11/24/09
to
Roger <rogerh...@gmail.com> writes:
> All,
>
> I have the below struct and I need to access the pointer to the "void
> * data" member without actually dereferencing the member variable. I
> could have gone with the approach of (ptr + sizeof(int) *4), but I
> dislike this idea - if I change something in the structure, I would
> have to revisit this part as well.
>
> struct something {
> int size;
> int msglen;
> int count;
> int handle;
> void *data;
> };
>
> struct something *ptr = (struct something *) msg;
>
> char *data_ptr = ptr + sizeof(int) * 4;

You're assuming, among other things, that there is no padding between
the members of the struct. This is quite likely to be true, but
it's not guaranteed -- and there's no need to assume it anyway.

ptr is of type "struct something *". Pointer arithmetic works
in units of the size of the pointed-to object, not in bytes.
So if sizeof(int) is, say, 4, then ptr + 16 will point 16 "struct
something"s past the location where ptr points.

Both "ptr" and "ptr + ..." are of type "struct something *".
You cannot legally use it to initialize a char*. (Some compilers
might let you off with just a warning, but it's a constraint
violation -- and you really don't need to do it anyway.)

The solution is much simpler than you think it is:

&ptr->data

or, if you're unsure about the relative precedence of "&" and "->":

&(ptr->data)

Note that this is of type void**, which you still can't legally
use to initialize a char*. Do you really want a char* pointer that
points to (the first byte of) a void* object? If so, you'll need a
cast, but that may be a sign that you need to rethink your design.
Probably data_ptr should be declared as void**, but there's not
enough information to tell.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Thomas Richter

unread,
Nov 24, 2009, 4:59:08 AM11/24/09
to
Roger wrote:
> All,
>
> I have the below struct and I need to access the pointer to the "void
> * data" member without actually dereferencing the member variable.

> struct something {


> int size;
> int msglen;
> int count;
> int handle;
> void *data;
> };

Where is the problem?

void **data_ptr = &(ptr->data).

Does not dereference ptr nor ptr->data.

So long,
Thomas

news

unread,
Nov 26, 2009, 3:46:41 PM11/26/09
to
Roger <rogerh...@gmail.com> writes:

> I have the below struct and I need to access the pointer to the "void
> * data" member without actually dereferencing the member variable. I
> could have gone with the approach of (ptr + sizeof(int) *4), but I
> dislike this idea - if I change something in the structure, I would
> have to revisit this part as well.

offsetof(struct something, data) would be easier to maintain.
However, I don't understand what you're really trying to do,
so I don't know whether offsetof is the best solution for you.
Note also that given the declaration struct something *ptr;
adding some integer x to ptr does not mean adding x bytes;
rather, it means adding x * sizeof(struct something) bytes.

0 new messages