对于结构体变量S1,S2,为什么我初始化" S1 = {25,3.5}",分配S2为" S1 = S2",但不能使用" S1 = {59,3.14}? [英] For struct variables s1,s2,why can I initialize "s1={25,3.5}",assign s2 as "s1=s2",but then can't use "s1={59,3.14}?

查看:205
本文介绍了对于结构体变量S1,S2,为什么我初始化" S1 = {25,3.5}",分配S2为" S1 = S2",但不能使用" S1 = {59,3.14}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,我们被允许,如果它们是相同的type.In按照如此,分配一个结构变量其他的价值在我下面的程序,我允许使用 S1 = S2 时,为何我不能使用 S1 = {59,3.14相同type.But的都是结构变量} 之后呢?

我知道我们不能比在一个字符串测试分配给一个字符数组改编其他初始化语句,因为字符串测试,它分解任务期间键入的char * ,因此有一种类型错配error.But在我的程序, {59,3.14} 不分解到任何指针,是吗?那么,为什么不允许被分配到 S1 即使是同一类型的,特别是因为它是在初始化过程中允许的?什么是 S2 和 {59,3.14} 这样的,一个是允许被分配到 S1 但对方是不是?

 #包括LT&;&stdio.h中GT;INT主要(无效)
{
    结构TEST1
    {
        int类型的;
        浮动B:
    } S1 = {} 25,3.5,S2 = {} 38,9.25;    的printf(%D,%F \\ N,s1.a,s1.b);    S1 = S2; //成功
    的printf(%D,%F \\ N,s1.a,s1.b);    S1 = {} 59,3.14; //错误:在{令牌预计前pression |
    的printf(%D,%F \\ N,s1.a,s1.b);}


解决方案

的C语法严格区分分配和初始化之间。

有关初始化显然是什么在右侧的类型应:该被声明的对象的类型。因此,初始符号是明确的; {A,B,C} 在声明顺序中的字段。

有关转让的事情都不太清楚。转让前pression X = Y 首先计算这两个SUBEX pressions( X ),着眼于它们的类型,然后进行必要的转换,如果可能的话,从到的类型类型 X 。表格的前pression {A,B,C} 没有类型,所以该机制无法正常工作。

这在他的回答yoones用途的结构是另外一种动物,称为复合文字。这是创建指定类型的一位不愿透露姓名的辅助对象的一种方式。您可以在初始化或者你想使用一个临时对象中的任何地方使用它。一个复合文字的存储类和寿命是从它使用的上下文推断。如果它是在功能范围内,它是(在栈)自动作为将是,将在同一个块中声明普通变量,只知道它不具有一个名称。如果在文件范围(一个全局变量的intialization,例如)是一种用于具有静态存储持续时间和寿命是程序执行的整个持续时间。

In C we are allowed to assign the value of one structure variable to other if they are of the same type.In accordance with that, in my following program I am allowed to use s1=s2 when both are struct variables of the same type.But why then I am not allowed to use s1={59,3.14} after that?

I know we can't assign a string "Test" to a character array arr other than in the initialization statement because for the string "Test",it decomposes to type char* during assignment and hence there is a type mismatch error.But in my program, {59,3.14} doesn't decompose to any pointer,does it?Why then it is not allowed to be assigned to s1 even though it is of same type,especially since it is allowed during the initialization?What is the different between s2 and {59,3.14} such that one is allowed to be assigned to s1 but the other is not?

#include<stdio.h>

int main(void)
{
    struct test1
    {
        int a;
        float b;
    } s1= {25,3.5},s2= {38,9.25};

    printf("%d,%f\n",s1.a,s1.b);

    s1=s2;   // Successful
    printf("%d,%f\n",s1.a,s1.b);

    s1= {59,3.14}; //ERROR:expected expression before '{' token|
    printf("%d,%f\n",s1.a,s1.b);

}

解决方案

The C grammar strictly distinguishes between assignment and initialization.

For initialization it is clear what the type on the right side ought to be: the type of the object that is declared. So the initializer notation is unambiguous; { a, b, c } are the fields in declaration order.

For assignment things are less clear. An assignment expression X = Y first evaluates both subexpressions (X and Y), looks at their types and then does the necessary conversions, if possible, from the type of Y to the type of X. An expression of the form { a, b, c } has no type, so the mechanism doesn't work.

The construct that yoones uses in his answer is yet another animal, called compound literal. This is a way of creating an unnamed auxiliary object of the specified type. You may use it in initializations or any other place where you'd want to use a temporary object. The storage class and lifetime of a compound literal is deduced from the context where it is used. If it is in function scope, it is automatic (on the "stack") as would be a normal variable that would be declared in the same block, only that it doesn't have a name. If it is used in file scope (intialization of a "global" variable, e.g) is has static storage duration and a lifetime that is the whole duration of the program execution.

这篇关于对于结构体变量S1,S2,为什么我初始化&QUOT; S1 = {25,3.5}&QUOT;,分配S2为&QUOT; S1 = S2&QUOT;,但不能使用&QUOT; S1 = {59,3.14}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆