【问题标题】:How to use structures in C ? I am finding it difficult to understand getting a lot of error while implementing it如何在 C 中使用结构?我发现在实现它时遇到很多错误很难理解
【发布时间】:2022-01-04 17:44:34
【问题描述】:

我正在尝试用 C 语言实现一个结构。我遇到了这些错误。

请帮我解决这个错误,并向我解释我做错了什么。

main.c:7:12: error: variable ‘s1’ has initializer but incomplete type
    7 |     struct student s1 = {"Nick",16,50,72.5};
      |            ^~~~~~~
main.c:7:26: warning: excess elements in struct initializer
    7 |     struct student s1 = {"Nick",16,50,72.5};
      |                          ^~~~~~
main.c:7:26: note: (near initialization for ‘s1’)
main.c:7:33: warning: excess elements in struct initializer
    7 |     struct student s1 = {"Nick",16,50,72.5};
      |                                 ^~
main.c:7:33: note: (near initialization for ‘s1’)
main.c:7:36: warning: excess elements in struct initializer
    7 |     struct student s1 = {"Nick",16,50,72.5};
      |                                    ^~
main.c:7:36: note: (near initialization for ‘s1’)
main.c:7:39: warning: excess elements in struct initializer
    7 |     struct student s1 = {"Nick",16,50,72.5};
      |                                       ^~~~
main.c:7:39: note: (near initialization for ‘s1’)
main.c:7:20: error: storage size of ‘s1’ isn’t known
    7 |     struct student s1 = {"Nick",16,50,72.5};

我的代码

#include<stdio.h>
#include<stdlib.h>

int main()
{
     
    struct student s1 = {"Nick",16,50,72.5};
    
    printf("%s",s1.name);
    
   // return 0;
}

struct student{
    
    char name[4];
    
    int age;
    
    int roll_no;
    
    float marks;
}s1;

【问题讨论】:

  • 编译器提示,您在使用前还没有定义struct student的成员。另外:char name[4]; 对于“Nick”来说太短了,几乎是任何名。

标签: c struct declaration definition incomplete-type


【解决方案1】:

就本声明而言

struct student s1 = {"Nick",16,50,72.5};

编译器不知道结构是如何定义的以及它有哪些数据成员。所以它发出消息。

您需要在声明结构类型的对象之前放置结构定义。例如

struct student{
    
    char name[4];
    
    int age;
    
    int roll_no;
    
    float marks;
};

int main( void )
{
    struct student s1 = {"Nick",16,50,72.5};
    //...

请注意,您正在尝试声明结构类型名称为 s1 的两个对象。

第一个在main中声明

struct student s1 = {"Nick",16,50,72.5};

和main之后文件范围内的第二个

struct student{
    
    char name[4];
    
    int age;
    
    int roll_no;
    
    float marks;
}s1;

在文件范围内删除此声明,并将结构定义放在 main 之前,如上所示。

【讨论】:

    【解决方案2】:

    C 编译器自上而下读取每个源文件。因此,当您尝试创建 struct student 类型的变量时,该类型尚未定义。

    在使用之前将结构定义移动到文件的顶部。

    【讨论】:

      【解决方案3】:

      正如 cmets 中所发布的,您的代码存在一些问题:

      • 当您尝试使用 student 时,编译器尚不知道 struct
      • char[4] 太短,无法容纳“Nick”,因为每个字符串中都有一个终止 NULL,并且为了在 printf() 中使用 name%s,您需要这个终止 0

      我将向您展示一个示例,其中包含一些声明和使用它的常用方法

      #include <stdio.h>
      #include <stdlib.h>
      
      typedef struct
      {
          char  name[15];
          int   age;
          int   roll_no;
          float marks;
      
      } Student;
      
      int main()
      {
          Student one;
          Student other = {"Nick", 16, 50, 72.5};
          Student modern_way =
          {
              .age = 12,
              .name = "James Bond 007"
          };
      
          one                = other; // one had no value yet
          Student a_group[3] =
          {
              [1] = one, 
              [0] = other, 
              [2] = modern_way
          };
      
          printf("%s, %d\n%s, %d\n",
              one.name, one.age,
              other.name, other.age
          );
      
          printf("%s, %d\n", modern_way.name, modern_way.age);
      
          // now a loop to list the ones in the array
          for (int i = 0; i < 3; i += 1)
              printf("%2d: %s, %d\n",
                  i,
                  a_group[i].name,
                  a_group[i].age);
          return 0;
      }
      

      输出

      Nick, 16
      Nick, 16
      James Bond 007, 12
       0: Nick, 16
       1: Nick, 16
       2: James Bond 007, 12
      

      在您看到的代码中,创建typedef Student 更加方便,因此您可以使用它而无需记住并在整个代码中重新输入struct

      命名事物的首字母大写的约定也很有用。

      one 已声明但未初始化,但稍后在代码中分配了一个值

      modern_way 以不那么新的 90 年代方式初始化,您可以命名字段并仅初始化您需要的字段,并且以任何顺序。

      a_group 向您展示如何处理初始化数组,并展示您甚至可以在代码中使用其他已知值。

      for 循环显示如何将它们全部列出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        • 2023-04-01
        • 2018-11-05
        • 2015-07-29
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        相关资源
        最近更新 更多