struct.field返回另一个值,为什么?

kab*_*oom 0 c struct

我的目标是创建一个fisrt自定义struct类型.运行时,打印出24.不明白为什么:

#include <stdio.h>
typedef struct strktura {
    int number;
    char name;
} strktura;

strktura new_one(int number, char name){
    strktura a;
    a.number=number;
    a.name=name;
}

main()
{
        strktura first=new_one(2,"A");
        printf("%d\n",first.number);
}
Run Code Online (Sandbox Code Playgroud)

Sou*_*osh 5

你忘了returnnew_one().

相关阅读:从第6.9.1章,第12段,C11文件,

如果到达终止函数的},并且调用者使用函数调用的值,则行为是未定义的.

因此,在您的代码中,没有returnfrom new_one()和by访问返回值printf("%d\n",first.number);,您将面临未定义的行为.

另外,值得一提的main()int main(),正确的语法是,(和匹配return 0是一种很好的做法.)