涉及功能时,结构,联合等的范围如何起作用?

时间:2019-02-02 16:28:29

标签: c

我最近遇到过结构,有些事情还不太清楚。假设我有一个这样的程序:

struct num print_a(struct num c); 

int main(){
  struct num{
    int a;
    int b;
  }c = {1, 2};
  struct num d;
  d = print_a(c);
}

struct num print_a(struct num c){
  printf("%d", c.a);
  return c;
}

这项工作还是我必须在main之外声明struct num?因为我的函数print_a如何在声明超出范围的num时“看到” struct num的含义(它必须返回的类型)?

很抱歉,如果问题是哑

2 个答案:

答案 0 :(得分:2)

  

将这项工作或做我必须声明的主要结构NUM外?

那么,为什么不简单地尝试一下呢?

http://ideone.com给出:

Compilation error #stdin compilation error #stdout 0s 9424KB
prog.c: In function ‘main’:
prog.c:11:15: error: type of formal parameter 1 is incomplete
   d = print_a(c);
               ^
prog.c:11:3: error: invalid use of undefined type ‘struct num’
   d = print_a(c);
   ^
prog.c:10:14: warning: variable ‘d’ set but not used [-Wunused-but-set-variable]
   struct num d;
              ^
prog.c: At top level:
prog.c:14:31: error: parameter 1 (‘c’) has incomplete type
 struct num print_a(struct num c){
                               ^
prog.c:14:12: error: return type is an incomplete type
 struct num print_a(struct num c){
            ^~~~~~~
prog.c:14:12: error: conflicting types for ‘print_a’
prog.c:3:12: note: previous declaration of ‘print_a’ was here
 struct num print_a(struct num c);
            ^~~~~~~
prog.c: In function ‘print_a’:
prog.c:16:10: warning: ‘return’ with a value, in function returning void
   return c;
          ^
prog.c:14:12: note: declared here
 struct num print_a(struct num c){
            ^~~~~~~

http://coliru.stacked-crooked.com/给出:

main.cpp: In function 'main':
main.cpp:13:15: error: type of formal parameter 1 is incomplete
   d = print_a(c);
               ^
main.cpp:13:7: error: invalid use of undefined type 'struct num'
   d = print_a(c);
       ^~~~~~~
main.cpp:12:14: warning: variable 'd' set but not used [-Wunused-but-set-variable]
   struct num d;
              ^
main.cpp: At top level:
main.cpp:16:31: error: parameter 1 ('c') has incomplete type
 struct num print_a(struct num c){
                    ~~~~~~~~~~~^
main.cpp:16:12: error: return type is an incomplete type
 struct num print_a(struct num c){
            ^~~~~~~
main.cpp:16:12: error: conflicting types for 'print_a'
main.cpp:5:12: note: previous declaration of 'print_a' was here
 struct num print_a(struct num c);
            ^~~~~~~
main.cpp: In function 'print_a':
main.cpp:18:10: warning: 'return' with a value, in function returning void
   return c;
          ^
main.cpp:16:12: note: declared here
 struct num print_a(struct num c){
            ^~~~~~~

嗯...那种给我们的答案:否 - 它不会工作

在main内部定义结构时,仅在main内部才知道。如果你想使用的结构外主,你必须移动结构的定义出的主 - 这样的:

struct num {
    int a;
    int b;
};

struct num print_a(struct num c); 

int main(){
  struct num c = {1, 2};
  struct num d;
  d = print_a(c);
}

struct num print_a(struct num c){
  printf("%d", c.a);
  return c;
}

答案 1 :(得分:2)

  1. 声明

    struct num print_a(struct num c);
    
    在示例代码开头的

    告诉编译器:

    • 您保证会在文件范围内定义struct num。的时刻,编译器标签struct num作为不完全型,其可以在使用(一些)的声明,例如,声明(但不定义)函数,并声明或定义 pointer 类型和变量。除非您实际上尝试使用类型struct num,否则编译器不会抱您这个诺言。

      必须在定义前或在其大小重要的声明中使用该类型来完成struct num 的定义。

    • 您承诺定义一个名为print_a的函数,该函数接受一个struct num并返回一个struct num。编译器不会抱着你这个承诺,除非你真正尝试使用此功能 - 如果你没有真正把它在任何地方的程序,编译器会原谅一个逝去的诺言

  2. struct num定义在函数内部main无关在文件范围内做的不完全struct num。你被允许继续前进,把它定义淡然的,但它的阴影的文件级struct num

  3. 声明

    d = print_a(c);
    

    内部功能main是不正确的,将导致编译器产生多个错误消息,因为它打破你做出的承诺。具体而言,编译器会抱怨你要使用的不完全类型struct num在文件范围内声明的,就好像它是完整的。请记住,struct num所定义内部main无关与struct num在文件级。

  4. 在函数定义

    struct num print_a(struct num c){ ... }
    

    是不正确的,因为您没有遵守诺言定义类型struct num。在main内还有另外一个名为struct num的类型 ;该类型仅在main内部可用。

要更正程序,您需要将struct num定义移到main之前。它可以在print_a声明之前或之后。