Structure Member Operator Error on Simple C Program

91 Views Asked by At

I'm studying about structures, and having some error. I cannot figure out why. Please help me out.

#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;
}

The compiler error is

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. Did I do definition, declaration, initialization or anything wrong?
  2. I cannot figure out what the error messages are saying. what do they mean?
0

There are 0 best solutions below