Incrementing a void pointer's member

J

John Leslie

Hi,

If I have:

struct foof {
long x;
};

void blarg(void)
{
struct foof blarg = {0};
void *vp = &blarg;
/* How do I then here increment vp->x, without using a
struct foof *? */
}

Regards,
 
J

Joona I Palaste

John Leslie said:
If I have:
struct foof {
long x;
};
void blarg(void)
{
struct foof blarg = {0};
void *vp = &blarg;
/* How do I then here increment vp->x, without using a
struct foof *? */
}

Is this a homework question? For some reason people tend to have a lot
of homework in the form "how can I do X without using Y".
You're in luck, as x is the first member of struct foof. Try this:
long *lp = vp;
(*lp)++;
If you're trying to access a member of the struct that is *not* the
first member, there's no portable way to do it without using a struct
pointer, because of padding.
 
I

Irrwahn Grausewitz

John Leslie said:
Hi,

If I have:

struct foof {
long x;
};

void blarg(void)
{
struct foof blarg = {0};
void *vp = &blarg;
/* How do I then here increment vp->x, without using a
struct foof *? */

Either use a type-cast:

++((struct foof *)vp)->x;

or use the correct data type instead of void *:

struct foof *p = &blarg;
++p->x;

Regards
 
J

John Leslie

Is this a homework question? For some reason people tend to have a lot
of homework in the form "how can I do X without using Y".

It isn't. The question resulted from:

cc: Warning: s_conf.c, line 635: In this statement, performing pointer
arithmetic on a pointer to void or a pointer to function is not allowed.
The compiler will treat the type as if it were pointer to char.
(badptrarith)

(Tru64's ccc)
You're in luck, as x is the first member of struct foof.

Sadly, it isn't, in the code i'm dealing with. Pasting code brevitly
does have its drawbacks. :)
Try this:
long *lp = vp;
(*lp)++;
If you're trying to access a member of the struct that is *not* the
first member, there's no portable way to do it without using a struct
pointer, because of padding.

Ah well :/

Thanks for the response.

Regards,
 
I

Irrwahn Grausewitz

Joona I Palaste said:
How exactly do either of these solutions not use a struct foof *, which
was the OP's requirement?

Ooops, was going way to fast, missed it; sorry. :}
 
C

CBFalconer

Joona said:
Is this a homework question? For some reason people tend to have a lot
of homework in the form "how can I do X without using Y".
You're in luck, as x is the first member of struct foof. Try this:
long *lp = vp;
(*lp)++;
If you're trying to access a member of the struct that is *not* the
first member, there's no portable way to do it without using a struct
pointer, because of padding.

A saner answer would be "you don't". Putting in those gyrations
does nothing more than obscure whatever is going on. There is no
discernible reason for the existance of vp. Declaring "struct foof
*sfp; sfp = &blarg;" might make some sense.
 
I

Irrwahn Grausewitz

Christopher Benson-Manica said:
OP liked it; all's well that ends well...

Asking for a doughnut and getting a muffin while passing
Bewleys at the speed of light isn't too bad, I guess... ;-)
 
G

Guillaume

struct foof {
long x;
};

void blarg(void)
{
struct foof blarg = {0};
void *vp = &blarg;
/* How do I then here increment vp->x, without using a
struct foof *? */
}

((struct foof *) vp)->x++;

Although, I don't see the point of making a void pointer if you're gonna
access the pointed structure members?

vp->x doesn't exist. I have a hard time figuring out what you're on,
there?
 
G

Guillaume

Is this a homework question? For some reason people tend to have a lot
of homework in the form "how can I do X without using Y".

Well, if it ever is homework, he should change teachers immediately.
Because not only does the question not make any sense, but it is
also misleading and an incentive for bad practices.
 
J

Jack Klein

It isn't. The question resulted from:

cc: Warning: s_conf.c, line 635: In this statement, performing pointer
arithmetic on a pointer to void or a pointer to function is not allowed.
The compiler will treat the type as if it were pointer to char.
(badptrarith)

(Tru64's ccc)

That means that you should study your compiler's options to operate it
in a more standard conforming mode. The first sentence of the message
is perfectly correct. The operation that the compiler performs is
extremely dubious, as the operation is just plain illegal in C.

The code should be rewritten properly.

There are games you can play with pointer casts and the standard
offsetof macro, but there is generally no excuse or justification for
this type of code.
 
D

Dan Pop

In said:
If you're trying to access a member of the struct that is *not* the
first member, there's no portable way to do it without using a struct
pointer, because of padding.

There is, by doing pointer arithmetic on a character pointer. The padding
is not a problem, because your program can store the offsets of the
members of interest (so that no offsetof macro is invoked in the
expression computing the actual pointer ;-)

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top