Could anyone define a C Macro for me?

shagpg2008 2012-01-01 08:10:55
Could anyone define a C Macro that includes the following function:
there is one pointer pdata, which is a struct
struct _data {
int m1;
int m2;
};

fun()
{
....

//I can use MACRO anywhere in a function.
MACRO(_data, int m1, int m2)
{
//Here I can use m1 & m2 directly,
//but not ((struct _data*)pdata)->m1 & ((struct _data*)pdata)->m2.
//assigned: ((struct _data*)pdata)->m1 ---> m1;
// ((struct _data*)pdata)->m2 ---> m2;

printf("m1 = %d, m2 = %d", m1, m2);
}

}


How can I define the MACRO ??

Thank you very much!
Happy new year!

...全文
156 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamnobody 2012-01-03
  • 打赏
  • 举报
回复
Come on,,,,

I don't wana try this with my poor C again..
shagpg2008 2012-01-03
  • 打赏
  • 举报
回复
Ok, thank you for your tips. I can handle it now.
Thank you all again for the warm heart.
mingliang1212: 30
jinglexy : 10
Any Problem?

Thank you all again.
七十二寨寨主 2012-01-02
  • 打赏
  • 举报
回复
没看明白你什么意思
#和##不能解决你的问题吗?
shagpg2008 2012-01-02
  • 打赏
  • 举报
回复
Oh, Great, very nice. mingliang1212 thank you very much for your tips.
And another thing: MACRO(pdata,int x,int y) is only one example, I'd like the MACRO is:

#define MACRO(pdata, ...)

So when we use it, MACRO(pdata,int x,int y) is an axample, and MACRO(pdata,struct _data x,char y) is anther axample. Though I know it's a very big challenge, Let's work it out.

Really big thanks to mingliang1212 .
iamnobody 2012-01-02
  • 打赏
  • 举报
回复



//I try to use C to implement this
struct _data {
int m1;
int m2;
};


int __COUNT;
#define GET_MEMBER_POS(struct_type,member_name) (int)(&(struct_type*)0->member_name)
#define MACRO(pdata,mem1,mem2) \
__COUNT = 1;\
for(mem1 = pdata->m1; __COUNT;)\
for(mem2 = pdata->m2; __COUNT;__COUNT--)


int main()
{

//how to use:
_data test;
_data* pdata = &test;
test.m1 = 12;
test.m2 = 23;

MACRO(pdata,int x,int y)
{
printf("test : m1 = %d,m2 = %d",x,y);

}


getchar();
}


shagpg2008 2012-01-02
  • 打赏
  • 举报
回复
谢谢你。 9楼

我要的不只是赋值,
宏的格式是:

MACRO(_data, int m1, int m2) {
.....
}

在宏里面实现对 m1, m2的赋值。注意, m1,m2是有类型的,是变量,不是常量。
Anyway, 还是谢谢你。

让大家一起来探讨吧!
iamnobody 2012-01-02
  • 打赏
  • 举报
回复


template<typename Type,typename MemberType,MemberType Type::*pMember>
struct FillMember
{
void operator()(Type &data,MemberType new_value)
{
data.*pMember = new_value;
}
};
///////////////////////////
//what you need to do with it is the following;
struct TestStruct
{
int x;
char y;
};

//you need the following two objects to help
FillMember<TestStruct,int,&TestStruct::x> fill_x;
FillMember<TestStruct,char,&TestStruct::y> fill_y;

#define FILL_TEST(test,x,y) \
do{fill_x(test,x);fill_y(test,y);}while(0)
int main()
{

//how to use:
TestStruct test;
fill_x(test,23);//fill x only;
fill_y(test,'y');

FILL_TEST(test,123,'a');//fill both;

printf("filled test: x = %d, y = %c",test.x,test.y);
getchar();
}

shagpg2008 2012-01-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 aquanull 的回复:]

如果要用这种语法通过m1、m2来修改dt.m1、dt.m2的值,需要C++的引用……

and mingliang1212: try C++;
[/Quote]

if C++ will be OK, could you help me to define the MACRO ?
I think it over and over, but can't help.


iamnobody 2012-01-02
  • 打赏
  • 举报
回复
C is too week in handling this.
try C++;

aquanull 2012-01-02
  • 打赏
  • 举报
回复
如果要用这种语法通过m1、m2来修改dt.m1、dt.m2的值,需要C++的引用……
shagpg2008 2012-01-01
  • 打赏
  • 举报
回复
如何实现这个宏:
MACRO
shagpg2008 2012-01-01
  • 打赏
  • 举报
回复
非常感谢您的回复!

可能是我没说清楚。

struct _data {
int m1;
int m2;
};


fun()
{
struct _data dt = {0, 1};
char * pdata = &dt;

.......

MACRO(_data, int m1, int m2) { //在这个宏里面完成各个赋值。
printf("m1 = %d, m2 = %d", m1, m2);
}

}
自信男孩 2012-01-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jinglexy 的回复:]
/**
* container_of - cast a member of a structure out to the containing structure
*
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @membe……
[/Quote]
++
拥剑公子 2012-01-01
  • 打赏
  • 举报
回复
/**
* container_of - cast a member of a structure out to the containing structure
*
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})


linux kernel里面的方法
shagpg2008 2012-01-01
  • 打赏
  • 举报
回复
自己顶下!呵呵

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧