C 拼图 - 玩类型

发布于 2024-08-10 21:49:00 字数 483 浏览 11 评论 0原文

请检查以下程序。

#include <stdio.h>

struct st
{
 int a ;
}

fn ()
{
 struct st obj ;
 obj.a = 10 ;

 return obj ;
}

int main()
{
 struct st obj = fn() ;

 printf ("%d", obj.a) ;
}

以下问题是

  1. 程序的输出是什么?
  2. “;”在哪里终止“struct st”的声明?

    通过 ISO IEC 9899 - 1999 规范、声明应 以“;”结尾。

     声明说明符 init-declarator-listopt ;
    
  3. 如果 'struct 的声明 st' 只代表返回类型 函数“fn”,它是如何可见的 到其他函数(主函数)?

Please check the below program.

#include <stdio.h>

struct st
{
 int a ;
}

fn ()
{
 struct st obj ;
 obj.a = 10 ;

 return obj ;
}

int main()
{
 struct st obj = fn() ;

 printf ("%d", obj.a) ;
}

Following are the questions

  1. What is the output of the program?
  2. Where is ';' terminating the declaration of 'struct st'?

    By ISO IEC 9899 - 1999
    specification, declaration should
    end with a ';'.

        declaration-specifiers init-declarator-listopt ;
    
  3. If the declaration of the 'struct
    st' is taken representing only the return type of
    the function 'fn', how is it visible
    to other functions (main)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-08-17 21:49:00
  1. 输出是 10。
  2. 不需要分号,因为整个事情就是一个函数定义。
  3. 结构体标记 st 是在全局范围内声明的,因此对 main 可见。
  1. The output is 10.
  2. There doesn't need to be a semicolon because the whole thing is a function definition.
  3. The structure tag st is declared at global scope and is therefore visible to main.
同尘 2024-08-17 21:49:00

如果我们重新格式化一下代码,事情可能会更清楚一些:

struct st { int a; } fn() 
{
  struct st obj;
  obj.a = 10;
  return obj;
}
int main()
{
  struct st obj = fn();
  printf("%d\n", obj.a);
  return 0;
}

因此,fn() 的返回类型是struct st {int a;}。结构体定义后没有分号,因为结构体类型是函数定义的一部分(从 translation-unit -> top-level-declaration -> 跟踪语法;函数定义)。结构类型可用于 main() 因为您在其上放置了一个结构标记 (st)。如果你这样写

struct { int a; } fn() {...}

,那么该类型将无法被 main() 使用;您必须创建具有相同定义的新结构类型。

你得到的效果和你写的一样

struct st {
  int a; 
};

struct st fn() 
{ 
  /* same as before */
}

int main()
{
  /* same as before */
}

Things may be a little clearer if we reformat the code a bit:

struct st { int a; } fn() 
{
  struct st obj;
  obj.a = 10;
  return obj;
}
int main()
{
  struct st obj = fn();
  printf("%d\n", obj.a);
  return 0;
}

Thus, the return type of fn() is struct st {int a;}. There's no semicolon after the struct definition because the struct type is part of the function definition (trace through the grammar from translation-unit -> top-level-declaration -> function-definition). The struct type is available to main() because you put a struct tag on it (st). Had you written

struct { int a; } fn() {...}

then the type would not have been available to main(); you would have had to create a new struct type with the same definition.

You get the same effect as if you had written

struct st {
  int a; 
};

struct st fn() 
{ 
  /* same as before */
}

int main()
{
  /* same as before */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文