473,327 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Incrementing a void pointer's member

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,

--
John
Nov 14 '05 #1
13 1978
John Leslie <jl*****@ptah.u.arizona.edu> scribbled the following:
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 *? */
}


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
Nov 14 '05 #2
John Leslie <jl*****@ptah.u.arizona.edu> wrote:
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
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #3
Irrwahn Grausewitz <ir*******@freenet.de> scribbled the following:
John Leslie <jl*****@ptah.u.arizona.edu> wrote:
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;

}


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane
Nov 14 '05 #4
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Irrwahn Grausewitz <ir*******@freenet.de> scribbled the following:
John Leslie <jl*****@ptah.u.arizona.edu> wrote: <snip>
/* 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;


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. :}
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #5
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
John Leslie <jl*****@ptah.u.arizona.edu> scribbled the following:
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 *? */
}
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,

--
John
Nov 14 '05 #6
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
John Leslie <jl*****@ptah.u.arizona.edu> wrote:
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;


Perfect! Thank you.

--
John
Nov 14 '05 #7
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
Ooops, was going way to fast, missed it; sorry. :}


OP liked it; all's well that ends well...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8
Joona I Palaste wrote:
John Leslie <jl*****@ptah.u.arizona.edu> scribbled the following:
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.


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
Ooops, was going way to fast, missed it; sorry. :}


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... ;-)
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #10
> 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?

Nov 14 '05 #11
> 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.
Nov 14 '05 #12
On Wed, 14 Apr 2004 18:00:40 +0000 (UTC), John Leslie
<jl*****@bast.u.arizona.edu> wrote in comp.lang.c:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
John Leslie <jl*****@ptah.u.arizona.edu> scribbled the following:
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 *? */
}

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)


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #13
In <c5**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alfonso Morra | last post by:
Straight of the bat, I'll admit this is not a nice solution, it is dangerous (not type safe) etc,etc, I know what the perils are. I don't need a lecture on why what I'm doing is perilous - that is...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
13
by: John Leslie | last post by:
Hi, If I have: struct foof { long x; }; void blarg(void) {
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.