这是我的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)