Accessing Structure Members - C Structure

C examples for Structure:Structure Definition

Description

Accessing Structure Members

Demo Code

#include <stdio.h>

typedef struct Horse Horse;            // Define Horse as a type name

struct Horse                           // Structure type definition
{
  int age;/*from  w w  w.j  ava 2s  .  c o  m*/
  int height;
  char name[20];
  char father[20];
  char mother[20];
};

int main(void)
{
  Horse my_horse;                      // Structure variable declaration

  printf("Enter the name of the horse: " );
  scanf("%s", my_horse.name, sizeof(my_horse.name));     // Read the name

  printf("How old is %s? ", my_horse.name );
  scanf("%d", &my_horse.age );                           // Read the age

  printf("How high is %s ( in hands )? ", my_horse.name );
  scanf("%d", &my_horse.height );                        // Read the height

  printf("Who is %s's father? ", my_horse.name );
  scanf("%s", my_horse.father, sizeof(my_horse.father)); // Get pa's name

  printf("Who is %s's mother? ", my_horse.name );
  scanf("%s", my_horse.mother, sizeof(my_horse.mother)); // Get ma's name

  // Now tell them what we know
  printf("%s is %d years old, %d hands high,",
                                 my_horse.name, my_horse.age, my_horse.height);
  printf(" and has %s and %s as parents.\n", my_horse.father, my_horse.mother);
  return 0;
}

Result


Related Tutorials