Learn C - C Struct






We can define a struct to declare a new data type.

We use struct keyword.

The struct keyword enables you to define a collection of variables of various types called a structure for a single unit.

The following code is a simple example of a structure declaration:

struct Dog { 
  int age; 
  int height; 
} aDog; 

This example declares a structure type called Dog.

This isn't a variable name; it's a new type.

This type name is referred to as a structure tag or a tag name.

The variable names within the Dog structure, age and height, are called members or fields.

The members of the structure appear between the braces that follow the struct tag name Dog.

In the example, an instance of the structure, called aDog, is declared.

aDog is a variable of type Dog.

aDog includes both members of the structure: the member age and the member height.

The following code adds more members to structure type Dog.

struct Dog 
{ 
  int age; 
  int height; 
  char name[20]; 
  char father[20]; 
  char mother[20]; 
} aDog = { 4, 1, "name", "C", "C++" }; 

There are five members to this version of the Dog structure type.

In the declaration of the variable aDog, the values that appear between the final pair of braces apply, in sequence, to the member variables age (4), height (1), name ( "name"), father ( "C"), and mother ( "C++").





Defining Structure Types and Structure Variables

You can define a structure type and a variable of a structure type in separate statements.

struct Dog { 
  int age; 
  int height; 
  char name[20]; 
  char father[20]; 
  char mother[20]; 
}; 

struct Dog aDog = { 4, 1,"name", "C", "C++" }; 

The first statement defines the structure tag Dog, and the second is a declaration of one variable of that type, aDog.

The following code defines another variable of type Dog:

struct Dog brother = { 3, 5, "new name", "C", "C++" };

You can declare multiple structure variables in a single statement.

struct Dog aDog, brother;

The struct keyword is required when you define a new variable that stores a structure.

You can remove struct by using a typedef definition.

For example:

typedef struct Dog Dog;

This defines Dog to be the equivalent of struct Dog.

If you put this definition at the beginning of a source file, you can define a variable of type Dog like this:

Dog t = { 3, 5, "new name", "C", "C++" };

The struct keyword is no longer necessary.





Accessing Structure Members

You refer to a member of a structure by writing the variable name followed by a period, followed by the member variable name.

aDog.age = 12;

The period is called the member selection operator.

This statement sets the age member of the structure referenced by aDog to 12.

You have the option of specifying the member names in the initialization list, like this:

Dog t = { .height = 5, 
               .age = 3, 
               .name = "name", 
               .mother = "C", 
               .father = "C++" };

The following code shows how to define a structure type, and assign value read from keyboard to store in it.


#include <stdio.h> 
  /*from   w  w w .  ja  va  2  s  . c o m*/
typedef struct Dog Dog;            // Define Dog as a type name 
  
struct Dog                           // Structure type definition 
{ 
  int age; 
  int height; 
  char name[20]; 
  char father[20]; 
  char mother[20]; 
}; 
  
int main(void) { 
  Dog my_Dog;                      // Structure variable declaration 
  
  // Initialize  the structure variable from input data 
  printf("Enter the name of the Dog: " ); 
  scanf("%s", my_Dog.name, sizeof(my_Dog.name));     // Read the name 
  
  printf("How old is %s? ", my_Dog.name ); 
  scanf("%d", &my_Dog.age );                           // Read the age 
  
  printf("How high is %s ( in hands )? ", my_Dog.name ); 
  scanf("%d", &my_Dog.height );                        // Read the height 
  
  printf("Who is %s's father? ", my_Dog.name ); 
  scanf("%s", my_Dog.father, sizeof(my_Dog.father)); 
  
  printf("Who is %s's mother? ", my_Dog.name ); 
  scanf("%s", my_Dog.mother, sizeof(my_Dog.mother)); 
  
  // Now tell them what we know 
  printf("%s is %d years old, %d hands high,", my_Dog.name, my_Dog.age, my_Dog.height); 
  printf(" and has %s and %s as parents.\n", my_Dog.father, my_Dog.mother); 
  return 0; 
} 

The code above generates the following result.

Example

For illustration, we define new data type, called employee.


#include <stdio.h> 
// w  w w  .  j a  v a  2  s . c om
// define a struct 
struct employee{ 
    int id; 
    char name[10]; 
    char country[5]; 
}; 

int main() { 

    // declare struct variable 
    struct employee emp; 

    // set values 
    emp.id = 10; 
    sprintf(emp.name,"jane"); 
    sprintf(emp.country,"DE"); 

    // display 
    printf("id: %d, name: %s, country : %s\n",emp.id,emp.name,emp.country); 

    return 0; 
} 

The code above generates the following result.

Arrays of Structures

The following code shows how to create an array of structures.


#include <stdio.h> 
#include <ctype.h> 
  /*w ww  .j  a v a 2 s.co m*/
typedef struct Dog Dog;            // Define Dog as a type name 
  
struct Dog                           // Structure type definition 
{ 
  int age; 
  int height; 
  char name[20]; 
  char father[20]; 
  char mother[20]; 
}; 
  
int main(void) 
{ 
  Dog my_Dogs[3];                 // Array of Dog elements 
  int hcount = 0;                 // Count of the number of Dogs 
  char test = '\0';               // Test value for ending 
  
  for(hcount = 0 ; hcount < sizeof(my_Dogs)/ sizeof(Dog) ; ++hcount) 
  { 
    printf("Do you want to enter details of a%s Dog (Y or N)? ", hcount ? "nother" : "" ); 
    scanf(" %c", &test, sizeof(test)); 
    if(tolower(test) == 'n') 
       break; 
  
    printf("Enter the name of the Dog: " ); 
    scanf("%s", my_Dogs[hcount].name, sizeof(my_Dogs[hcount].name)); 
  
    printf("How old is %s? ", my_Dogs[hcount].name ); 
    scanf("%d", &my_Dogs[hcount].age); 
  
    printf("How high is %s ( in hands )? ", my_Dogs[hcount].name); 
    scanf("%d", &my_Dogs[hcount].height); 
  
    printf("Who is %s's father? ", my_Dogs[hcount].name); 
    scanf("%s", my_Dogs[hcount].father, sizeof(my_Dogs[hcount].father)); 
  
    printf("Who is %s's mother? ", my_Dogs[hcount].name); 
    scanf("%s", my_Dogs[hcount].mother, sizeof(my_Dogs[hcount].mother)); 
  } 
  // Now tell them what we know. 
  printf("\n"); 
  for (int i = 0 ; i < hcount ; ++i) { 
    printf("%s is %d years old, %d hands high,", my_Dogs[i].name, my_Dogs[i].age, my_Dogs[i].height); 
    printf(" and has %s and %s as parents.\n", my_Dogs[i].father, my_Dogs[i].mother); 
  } 
  return 0; 
 
}

The code above generates the following result.