[圖解C] Example 904: pointer to struct

// Example 904: pointer to struct
// 2 ways to access

#include <stdio.h>
#include <stdlib.h>

int main()
{
    struct animal
    {
        float weight;
        int age;
    }tiger = {180, 3};
 
    // declare a pointer to struct
    struct animal *ptr;
    ptr = &tiger;   // deposit address of tiger in ptr
 
    // way 1 to access the member of struct: pointer -> variable
    // way 2 to access the member of struct: *
    printf("weight: %.2f\n", ptr -> weight);
    printf("weight: %d\n", (*ptr).age);
 
    system("pause");
    return 0;
}