增加void指针的成员 [英] Incrementing a void pointer's member

查看:57
本文介绍了增加void指针的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我有:


struct foof {

long x;

};


void blarg(无效)

{

struct foof blarg = {0};

void * vp =& blarg;

/ *我如何在这里增加vp-> x,而不使用

struct foof *? * /

}


问候,


-

John

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

推荐答案

John Leslie< jl ***** @ ptah.u.arizona.edu>潦草地写道:
John Leslie <jl*****@ptah.u.arizona.edu> scribbled the following:

如果我有:
struct foof {
long x;
};
void blarg(void)
{bofg = {0};
void * vp =& blarg;
/ *我如何在这里递增vp-> x,没有使用
struct foof *? * /
}
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 *? */
}




这是一个功课问题吗?出于某种原因,人们倾向于以我怎么能不使用Y而做X的形式有很多

的作业。

你很幸运,如同x是struct foof的第一个成员。试试这个:

long * lp = vp;

(* lp)++;

如果你想要访问一个成员结构是*不是*

第一个成员,没有使用结构

指针,因为填充,没有可移植的方法。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)----------- - 芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

冰淇淋销售不知何故导致溺水:两者都发生在夏季。

- Antti Voipio& Arto Wikla



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


John Leslie< jl ***** @ ptah.u.arizona.edu>写道:
John Leslie <jl*****@ptah.u.arizona.edu> wrote:


如果我有:

struct foof {
long x;
};

void blarg(void)
{bofg = {0};
void * vp =& blarg;
/ *我如何然后这里增加vp-> x,而不使用
struct foof *? * /


要么使用类型转换:


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


或使用正确的数据类型而不是void *:


struct foof * p =& blarg;

++ p-> x;

}
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;
}




问候

-

Irrwahn Grausewitz(ir*******@freenet.de)

欢迎来到clc: http://www.ungerhu.com/jxh/clc.welcome.txt

clc faq-list: http://www.faqs.org/faqs/C-faq/faq /

clc OT指南: http://benpfaff.org/writings/clc/off-topic.html



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


Irrwahn Grausewitz< ir ******* @ freenet.de>潦草地写下:
Irrwahn Grausewitz <ir*******@freenet.de> scribbled the following:
John Leslie< jl ***** @ ptah.u.arizona.edu>写道:
John Leslie <jl*****@ptah.u.arizona.edu> wrote:


如果我有:

struct foof {
long x;
};

void blarg(void)
{bofg = {0};
void * vp =& blarg;
/ *我如何然后这里增加vp-> x,而不使用
struct foof *? * /
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 *? */
使用类型转换:
++((struct foof *)vp) - > x;
或使用正确的数据类型而不是void *:
struct foof * p =& blarg;
++ p-> x;
Either use a type-cast: ++((struct foof *)vp)->x; or use the correct data type instead of void *: struct foof *p = &blarg;
++p->x;



}


}




这些解决方案中的任何一个都不使用struct foof *,其中

是OP'要求?


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)----- --------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

我们怎样才能用性来获得我们想要的东西?性是我们想要的。

- Frasier Crane博士



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


这篇关于增加void指针的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆