www.un.org/Depts/DGACM/index_spanish.htm 我对我手法中的错误感到担忧,现在我看到这两种产出都是一样的。 下面是经过编辑的版本。
我要说的是,我有一个结构:
typedef struct
{
char m[5];
char f[6];
} COUPLE;
我读成一个阵列的文件:
char *data = malloc(11);
FILE *f = fopen("myfile", "rb");
fread(data, 1, 11, f);
fclose(f);
我总是在需要从一阵列中填满我的结构时使用这一守则:
COUPLE titanic;
memcpy(&titanic, data, sizeof(data));
printf("%s and %s", titanic.m, titanic.f);
这一工作非常出色,但事实上,我的边际阵列可能非常大,因此,我试图优化我的法典。
COUPLE *titanic = (COUPLE *)data;
printf("%s and %s", titanic->m, titanic->f);
因此,我的问题是:
- (obsolete) Why do I get different outputs?
- (obsolete) How can I fill a structure just by casting from an array?
- Should I avoid this kind of optimization?
- Are there possible pitfalls in it?