由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Questions about c code
相关主题
请问可以这样定义struct吗?difference between FILE and struct FILE
One question about Void pointer[合集] Interview Question
请问const myClass &src 和myClass const &src有什么区别?ask a question about struct in C programming
Question: Given a pointer to a functiontypedef
how to initialize this struct.typedef struct的问题
为啥有人喜欢把_s结尾的结构typedef成_t结尾的,有讲究么?一个简单的C编程问题
sizeof()的问题有段c++代码看不懂
请教函数 INIT 怎么能free memory请教一个c++ 里functor的问题
相关话题的讨论汇总
话题: mathead话题: questions话题: code话题: what话题: does
进入Programming版参与讨论
1 (共1页)
y***6
发帖数: 46
1
Hi All,
I just started learning C programming. Can someone help me with the
following C code? Thanks!
------------C code starts here-------------
typedef struct {
int row;
int col;
} MATHEAD;
#define Mathead(a) ((MATHEAD *) (MATHEAD *)(a)-1))
------------C code ends here--------------
My questions are:
i) What does (MATHEAD *)(a)-1 mean? Why minus 1?
ii) What does ((MATHEAD *) (MATHEAD *)(a)-1)) return?
t****t
发帖数: 6806
2
if you can't type, do use copy/paste. the parenthesis in your macro is not
matched.

【在 y***6 的大作中提到】
: Hi All,
: I just started learning C programming. Can someone help me with the
: following C code? Thanks!
: ------------C code starts here-------------
: typedef struct {
: int row;
: int col;
: } MATHEAD;
: #define Mathead(a) ((MATHEAD *) (MATHEAD *)(a)-1))
: ------------C code ends here--------------

y***6
发帖数: 46
3
Thanks for pointing that out. I have re-edited my post.
t****t
发帖数: 6806
4
basically i believe you put a MATHEAD structure immediately before your real
data. if A points to your real data; then macro Mathead(A) returns the
pointer to the MATHEAD right before your data.
but i don't think that macro makes sense to cast (a) twice into (MATHEAD*).
it doesn't hurt though.

【在 y***6 的大作中提到】
: Hi All,
: I just started learning C programming. Can someone help me with the
: following C code? Thanks!
: ------------C code starts here-------------
: typedef struct {
: int row;
: int col;
: } MATHEAD;
: #define Mathead(a) ((MATHEAD *) (MATHEAD *)(a)-1))
: ------------C code ends here--------------

m*******e
发帖数: 20
5
(i) (MATHEAD *)(a)-1 means you explictely convert pointer "a" into a MATHEAD
type pointer("a" is very likely to be a void* pointer originally), and then
make the pointer point to the memory place immediately before the current
place it does.
(ii) Not very sure about your second question. Actually I think such macro
does not make any sense...Well hope somebody can give out a more precise
answer.

【在 y***6 的大作中提到】
: Hi All,
: I just started learning C programming. Can someone help me with the
: following C code? Thanks!
: ------------C code starts here-------------
: typedef struct {
: int row;
: int col;
: } MATHEAD;
: #define Mathead(a) ((MATHEAD *) (MATHEAD *)(a)-1))
: ------------C code ends here--------------

a***y
发帖数: 2803
6
1个int有4个byte,所以一个struct MATHEAD就有8个字节.
(MATHEAD *)(a)-1表示的是减去一个struct单位的地址,也就是减去8个字节.
这是指针类的加减法的特殊的地方.

【在 y***6 的大作中提到】
: Hi All,
: I just started learning C programming. Can someone help me with the
: following C code? Thanks!
: ------------C code starts here-------------
: typedef struct {
: int row;
: int col;
: } MATHEAD;
: #define Mathead(a) ((MATHEAD *) (MATHEAD *)(a)-1))
: ------------C code ends here--------------

1 (共1页)
进入Programming版参与讨论
相关主题
请教一个c++ 里functor的问题how to initialize this struct.
问个编程,系统,网络有关的综合问题。为啥有人喜欢把_s结尾的结构typedef成_t结尾的,有讲究么?
typedef 的一个问题sizeof()的问题
请教一个rpcgen的问题请教函数 INIT 怎么能free memory
请问可以这样定义struct吗?difference between FILE and struct FILE
One question about Void pointer[合集] Interview Question
请问const myClass &src 和myClass const &src有什么区别?ask a question about struct in C programming
Question: Given a pointer to a functiontypedef
相关话题的讨论汇总
话题: mathead话题: questions话题: code话题: what话题: does