hope_process
heixia108_cu
全部博文(61)
oldlinux(0)
Perl(1)
学习资料(0)
Enhlish(0)
心情(5)
Windows(0)
硬件相关(0)
软件工程(0)
数据挖掘与AI(0)
OpenGL(1)
2009年(2)
2008年(59)
Bsolar
zhanghan
anhuiwen
luoqeng
分类: C/C++
2009-04-25 02:01:50
#include <stdio.h> #include <stdlib.h> #include <string.h> struct _t { size_t size; char s[]; }; int main() { struct _t *t; t = (struct _t *)malloc(sizeof (struct _t) + 10 * sizeof (char)); if (!t) { fputs("Out of memory.", stderr); return 1; } t->size = 10; strncpy(t->s, "1234567891234567891234", t->size - 1); t->s[t->size-1] = '\0'; puts(t->s); free(t); return 0; }
上一篇:注意 c++ vector中的erase()
下一篇:Google hack
chinaunix网友2009-05-12 22:29:27
在char s[]; 的括号里加上任意非负整数都是可以的,应该不能为0(为0的意义是什么呢,c不允许吧)
chinaunix网友2009-05-11 16:48:52
唔,修改的话,malloc(sizeof (struct _t)要改为malloc(sizeof (size_t)
chinaunix网友2009-05-11 16:46:57
忐忑不安地留言: 这个好像毫无意义,在char s[]; 的括号里加上任意非负整数都是可以的,比如0,不影响后面程序。 是不是?
登录 注册