c - 简单 C 程序中的结构成员运算符错误
问题描述
我正在研究结构,并且有一些错误。我不知道为什么。请帮帮我。
#include <stdio.h>
struct data{
int x;
float y;
float z;
} info;
info.x = 100;
struct data *ptr;
ptr = &info;
(*ptr).y = 5.5;
(*ptr).z = 1.0;
int main(void)
{
printf("The first member's value is %d.\n", info.x);
printf("The second member's value is %.1f.\n", info.y);
printf("The third member's value is %.1f.\n", info.z);
return 0;
}
编译器错误是
exercise112.c:10:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
info.x = 100;
^
exercise112.c:13:1: warning: data definition has no type or storage class
ptr = &info;
^~~
exercise112.c:13:1: warning: type defaults to ‘int’ in declaration of ‘ptr’ [-Wimplicit-int]
exercise112.c:13:1: error: conflicting types for ‘ptr’
exercise112.c:12:14: note: previous declaration of ‘ptr’ was here
struct data *ptr;
^~~
exercise112.c:13:7: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
ptr = &info;
^
exercise112.c:13:7: error: initializer element is not computable at load time
exercise112.c:15:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
(*ptr).y = 5.5;
^
exercise112.c:16:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
(*ptr).z = 1.0;
^
- 我做了定义、声明、初始化还是做错了什么?
- 我无法弄清楚错误消息在说什么。他们的意思是什么?
解决方案
推荐阅读
- java - 松鼠不会运行 (ClassNotFoundException : TextureLookAndFeel )
- javascript - 是否可以在不提供空值数组的情况下在 billboard.js 时间序列图表上添加一个点?
- dc.js - 如何使用 crossfilter 和 dc.js 开发分组堆栈条形图
- python - Python:根据分组生成频率(总和和计数)
- security - OpenAPI 定义是否应该公开?
- android - Lint 在组装发布目标时发现致命错误,添加语言 xml 文件
- php - unlink(/home/goode/public_html/private/uploads/405/):是一个目录,虽然file_exist被检查
- angular - 使用嵌套的 for 循环了解 Angular 的生命周期钩子和模板
- php - 使用 PHP 和 SQL Server 的登录脚本
- kentico - 扩展管理员用户网格 Kentico 10 的列