【发布时间】:2015-03-06 09:14:53
【问题描述】:
今天我得到了一些信息,我们可以通过这种方式获取结构中字段的偏移量:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
struct sdshdr {
int len;
int free;
};
int main(int argc, char* argv[])
{
printf("%d\n", &sdshdr::len);
printf("%d\n", &sdshdr::free);
}
虽然我在编译时收到了警告,但它可以成功运行。 我们该如何解释呢?我在网上搜索时没有得到信息。 谁能帮忙解释一下这里发生了什么?
编译参数:gcc -g -O2 -Wall -o main.o main.cpp
【问题讨论】:
-
使用哪些编译器标志以及您正在构建该程序的编译器?
-
不,错误:在 ':' 标记 codepad.org/ZsNJY08g 之前需要 ')'
-
gcc -g -O2 -Wall -o main.o main.cpp
-
对不起,我在文本中更新了整个程序和编译器标志
-
在我的电脑上编译不了,
::是C++神器,使用printf("%zu\n", offsetof(struct sdshdr, len));