首页 > 解决方案 > 简单 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;
       ^
  1. 我做了定义、声明、初始化还是做错了什么?
  2. 我无法弄清楚错误消息在说什么。他们的意思是什么?

标签: cstructuredot-operator

解决方案


推荐阅读