C - 错误:'a'的存储大小未知

use*_*697 21 c gcc struct

这是我的C程序......

#include <stdio.h>

struct xyx {
    int x;
    int y;
    char c;
    char str[20];
    int arr[2];
};

int main(void)
{
    struct xyz a;
    a.x = 100;
    printf("%d\n", a.x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误....

按ENTER或键入命令继续

13structtest.c: In function ‘main’:
13structtest.c:13:13: error: storage size of ‘a’ isn’t known
13structtest.c:13:13: warning: unused variable ‘a’ [-Wunused-variable]

ta.*_*.is 24

您的结构被调用struct xyx但是a类型struct xyz.一旦你修复了它,输出就是100.

#include <stdio.h>

struct xyx {
    int x;
    int y;
    char c;
    char str[20];
    int arr[2];
};

int main(void)
{
    struct xyx a;
    a.x = 100;
    printf("%d\n", a.x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Vin*_*kla 11

在这种情况下,用户在定义和使用上犯了错误。如果有人对某个结构做了typedef同样的操作,则应该使用相同的结构,而不使用struct以下示例。

typedef struct
{
   int a;
}studyT;
Run Code Online (Sandbox Code Playgroud)

在函数中使用时

int main()
{
   struct studyT study; // This will give above error.
   studyT stud; // This will eliminate the above error.
   return 0;
}
Run Code Online (Sandbox Code Playgroud)


小智 10

对于遇到此问题的任何人来说,这是一个拼写错误。检查您的 struct delcerations 和您的 struct 的拼写


小智 5

您将结构定义为 xyx,但您正在尝试创建名为 xyz 的结构。