一个浮点数在C中打印正常,另一个不';T
运行以下操作的结果:一个浮点数在C中打印正常,另一个不';T,c,printing,floating-point,C,Printing,Floating Point,运行以下操作的结果: struct drinks{ float alcohol; float price; char name[1]; }; /*stackoverflow.com/questions/10162152/how-to-work-with-string-fields-in-a-c-struct*/ void main(){ struct drinks cosmo; //The following: cosmo.alcohol =
struct drinks{
float alcohol;
float price;
char name[1];
};
/*stackoverflow.com/questions/10162152/how-to-work-with-string-fields-in-a-c-struct*/
void main(){
struct drinks cosmo;
//The following:
cosmo.alcohol = 20.67;
//gives some crazy result when printed ! Says alcohol content is 2345826759803.00000000 (or any random number like that)
cosmo.price = 10.0;
char name[] = "Cosmopolitan";
size_t length = strlen(name);
realloc(cosmo.name,length);
strcpy(cosmo.name, name);
printf("The alcohol content of the %s is %4.2f, and it costs %2.f",cosmo.name, cosmo.alcohol, cosmo.price);
}
这正是它应该是什么(饮料的名称和价格打印罚款!)除了酒精含量的饮料打印作为一个荒谬的数字
另外,代码的主题是胡说八道,我只是在学习,并决定想出任何东西
编辑:成功了!我使用了简单的修复,谢谢!我现在可以问一个问题了吗?我想问一个技术问题,为什么分配给“name”的内存对打印饮料酒精含量的正确浮点值有任何影响?您有一个字段
name
,它是结构中大小为1的字符数组
您执行了realloc(cosmo.name,length)代码>这是未定义的行为,可以解释这些数字是“荒谬的”
您的结构应该为字符串定义更大的大小
struct drinks{
float alcohol;
float price;
char name[100];};
或者定义一个指针(将其设置为NULL
以便realloc
工作,或者使用malloc
):
当您realloc/malloc
时,不要忘记字符串终止符(+1)或使用strdup
cosmo.name = malloc(1+length);
strcpy(cosmo.name, name);
或
您有一个字段name
,它是结构中大小为1的字符数组
您执行了realloc(cosmo.name,length)代码>这是未定义的行为,可以解释这些数字是“荒谬的”
您的结构应该为字符串定义更大的大小
struct drinks{
float alcohol;
float price;
char name[100];};
或者定义一个指针(将其设置为NULL
以便realloc
工作,或者使用malloc
):
当您realloc/malloc
时,不要忘记字符串终止符(+1)或使用strdup
cosmo.name = malloc(1+length);
strcpy(cosmo.name, name);
或
有两个问题:第一个问题是structuresname
成员是一个数组,其大小在编译时是固定的。你不能重新分配。调用realloc
将导致未定义的行为
下一个问题是,然后将一个to长字符串复制到单元素数组中,从而写出界,并且再次具有未定义的行为
一个显而易见且简单的解决方案是首先创建一个更大的数组。您还可以将名称
成员转换为指针,在这种情况下,您可以为其分配内存(使用malloc
或strdup
)。如果选择第二种解决方案并使用malloc
,请记住C中的字符串有一个终止符。因此像“hello”
这样的字符串有六个元素:您看到的五个元素,它们用strlen
加上终止符来计数。有两个问题:第一个问题是结构名称
成员是一个数组,在编译时其大小是固定的。你不能重新分配。调用realloc
将导致未定义的行为
下一个问题是,然后将一个to长字符串复制到单元素数组中,从而写出界,并且再次具有未定义的行为
一个显而易见且简单的解决方案是首先创建一个更大的数组。您还可以将名称
成员转换为指针,在这种情况下,您可以为其分配内存(使用malloc
或strdup
)。如果选择第二种解决方案并使用malloc
,请记住C中的字符串有一个终止符。所以像“hello”
这样的字符串有六个元素:你看到的五个元素,它们用strlen
加上终止符来计数。现代C语言中正确的方法是:
typedef struct {
float alcohol;
float price;
char name[];
} drinks;
...
const char str NAME[] = "Cosmopolitan";
drinks* cosmo = malloc(sizeof(*cosmo) + sizeof(NAME));
cosmo->alcohol = 20.67f;
cosmo->price = 10.0f;
memcpy(cosmo->name, NAME, sizeof(NAME));
// (strcpy is fine too, but is slightly slower)
请注意,所有的sizeof
操作符调用都考虑了空终止符,因为它们是C字符串。在现代C中,正确的方法是:
typedef struct {
float alcohol;
float price;
char name[];
} drinks;
...
const char str NAME[] = "Cosmopolitan";
drinks* cosmo = malloc(sizeof(*cosmo) + sizeof(NAME));
cosmo->alcohol = 20.67f;
cosmo->price = 10.0f;
memcpy(cosmo->name, NAME, sizeof(NAME));
// (strcpy is fine too, but is slightly slower)
请注意,所有的sizeof
操作符调用都考虑了空终止符,因为它们是C字符串。如果要使用C99和灵活数组成员(FAM)淘汰的技术,这不是使用“struct hack”的方式,然后必须分配整个结构和字符数组。你不能像那样只重新分配结构类型的一部分。(在其他结构中,在其他情况下(例如结构中的char*name;
),它是可以的;如果char name[1];
这样,它是不可以的。)@JonathanLeffler我认为从来没有正确使用“结构黑客”的方法。哦,拜托,让我们不要发明更多与C相关的TLA!这不是使用“struct hack”的方式——如果要使用C99和灵活数组成员(FAM)淘汰的技术,那么必须分配整个结构和字符数组。你不能像那样只重新分配结构类型的一部分。(在其他结构中,在其他情况下(例如结构中的char*name;
),它是可以的;如果char name[1];
这样,它是不可以的。)@JonathanLeffler我认为从来没有正确使用“结构黑客”的方法。哦,拜托,让我们不要发明更多与C相关的TLA!编辑:成功了!我使用了简单的修复,谢谢!我现在可以问一下,并且可以自由地了解一下技术,为什么分配给“name”的内存对打印饮料酒精含量的正确浮动值有任何影响?编辑:它起作用了!我使用了简单的修复,谢谢!我现在可以问一个问题了吗?我可以随便问一下技术问题,为什么分配给“name”的内存会对打印饮料酒精含量的正确浮动值产生任何影响?