免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: maxxfire
打印 上一主题 下一主题

世界真奇妙,这个也会出错。。 [复制链接]

论坛徽章:
0
41 [报告]
发表于 2008-09-24 23:31 |只看该作者
  1. struct T{
  2.         char *name;
  3.         int *id;
  4. };

  5. int a;
  6. int* p=&a;
  7. char* s="suck";
  8. struct T t1 = {
  9.         "fuck",
  10.         &a
  11. };

  12. int main()
  13. {
  14.         t1.id=p;
  15.         t1.name=s;
  16.         printf("asshole\n");
  17. }
复制代码


以上是我测试通过的写法


  1. $ gcc -v
  2. 使用内建 specs。
  3. 目标:i386-redhat-linux
  4. 配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-cpu=generic --build=i386-redhat-linux
  5. 线程模型:posix
  6. gcc 版本 4.3.0 20080428 (Red Hat 4.3.0-8) (GCC)
复制代码

论坛徽章:
0
42 [报告]
发表于 2008-09-24 23:41 |只看该作者
答案就在标准里:

C99标准 6.7.8 Initialization 第4款:
4 All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

关于 static storage duration:
C99 6.2.4 Storage durations of objects 第3款:
3 An object whose identifier is declared with external or internal linkage, or with the
storage-class specifier static has static storage duration. Its lifetime is the entire
execution of the program and its stored value is initialized only once, prior to program startup.

关于复杂类型 (比如struct):
C99 6.5.2.5 Compound literals 第7款:
7 All the semantic rules and constraints for initializer lists in 6.7.8 are applicable to
compound literals.

论坛徽章:
0
43 [报告]
发表于 2008-09-24 23:55 |只看该作者

回复 #42 emacsnw 的帖子

谢谢,学习了。

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
44 [报告]
发表于 2008-09-25 00:04 |只看该作者
原帖由 emacsnw 于 2008-9-24 23:41 发表
答案就在标准里:

C99标准 6.7.8 Initialization 第4款:
4 All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
...

谁能翻译一下

论坛徽章:
0
45 [报告]
发表于 2008-09-25 00:27 |只看该作者
原帖由 ynchnluiti 于 2008-9-24 08:04 发表

谁能翻译一下


汗,好像都是C里最常用的术语。你那个字不认识?

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
46 [报告]
发表于 2008-09-25 00:46 |只看该作者
单词都认识。不理解:
int a=1;
int *p=&a;
中p不是constant。

论坛徽章:
0
47 [报告]
发表于 2008-09-25 09:10 |只看该作者
完全看不懂

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
48 [报告]
发表于 2008-09-25 09:17 |只看该作者
原帖由 net_robber 于 2008-9-24 23:31 发表
struct T{
        char *name;
        int *id;
};

int a;
int* p=&a;
char* s="suck";
struct T t1 = {
        "fuck",
        &a
};

int main()
{
        t1.id=p;
        t1.name=s;
        printf("asshole\n");
}


以上是我测 ...


为什么t1在main之前已经初始化了,main里面再去赋值呢?

论坛徽章:
0
49 [报告]
发表于 2008-09-25 09:22 |只看该作者
变量初始化的问题。领教了。

[ 本帖最后由 Siete 于 2008-9-25 09:39 编辑 ]

论坛徽章:
0
50 [报告]
发表于 2008-09-25 09:33 |只看该作者
原帖由 emacsnw 于 2008-9-24 23:41 发表
答案就在标准里:

C99标准 6.7.8 Initialization 第4款:
4 All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
...


哦,谢谢,原来是这样。。
也就是就说做为全局变量,在编译之后就要确定它的地址,如果这个全局变量是个结构体的话,那么结构体里面的各个成员的具体值也要确定了。
因为全局变量是存储在静态区,而只有常量表达式才可以前期确定值,而不是具体运行时确定值。
这么说g++之所以可以编译通过,那是它的链接器有优化,做了后期绑定。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP