Holla Tech - Learn

Structures
 

A structure is a user-defined data type that groups related variables of different data types.

A structure declaration includes the keyword struct, a structure tag for referencing the structure, and curly braces { } with a list of variable declarations called members.
For example: 

struct course {
  int id;
  char title[40];
  float hours;
}; 

 

This struct statement defines a new data type named course that has three members.
Structure members can be of any data type, including basic types, strings, arrays, pointers, and even other structures, as you will learn in a later lesson.

NOTE!
Do not forget to put a semicolon after structure declaration. A structure is also called a composite or aggregate data type. Some languages refer to structures as records.

Declarations Using Structures
 

To declare variables of a structure data type, you use the keyword struct followed by the struct tag, and then the variable name.
For example, the statements below declares a structure data type and then uses the student struct to declare variables s1 and s2:

struct student {
  int age;
  int grade;
  char name[40];
};

/* declare two variables */
struct student s1;
struct student s2

 

NOTE!
A struct variable is stored in a contiguous block of memory. The sizeof operator must be used to get the number of bytes needed for a struct, just as with the basic data types.

Declarations Using Structures
 

A struct variable can also be initialized in the declaration by listing initial values in order inside curly braces:

struct student s1 = {19, 9, “John”};
struct student s2 = {22, 10, “Batman”}; 

 

If you want to initialize a structure using curly braces after declaration, you will also need to type cast, as in the statements:

struct student s1;
s1 = (struct student) {19, 9, “John”}; 

 

You can use named member initialization when initializing a structure to initialize corresponding members:

struct student s1
= { .grade = 9, .age = 19, .name = “John”}; 

 

In the example above, .grade refers to the grade member of the structure. Similarly, .age and .name refer to the age and name members.


Accessing Structure Members
 

You access the members of a struct variable by using the . (dot operator) between the variable name and the member name.
For example, to assign a value to the age member of the s1 struct variable, use a statement like:

s1.age = 19

You can also assign one structure to another of the same type:

struct student s1 = {19, 9, “Jason”};
struct student s2;
//….
s2 = s1

 

The following code demonstrates using a structure:

#include <stdio.h>
#include <string.h>

struct course {
  int id;
  char title[40];
  float hours;
};

int main() {
  struct course cs1 = {341279, “Intro to C++”, 12.5};
  struct course cs2;

  /* initialize cs2 */
  cs2.id = 341281;
  strcpy(cs2.title, “Advanced C++”);
  cs2.hours = 14.25;
  
  /* display course info */
  printf(“%d\t%s\t%4.2f\n”, cs1.id, cs1.title, cs1.hours);
  printf(“%d\t%s\t%4.2f\n”, cs2.id, cs2.title, cs2.hours);
 
  return 0;

 

String assignment requires strcpy() from the string.h library.
Also note the format specifiers %4.2f include width and precision options.  


Using typedef
 

The typedef keyword creates a type definition that simplifies code and makes a program easier to read.
typedef is commonly used with structures because it eliminates the need to use the keyword struct when declaring variables.
For example:

typedef struct {
  int id;
  char title[40];
  float hours;
} course;

course cs1;
course cs2

 

NOTE!
Note that a structure tag is no longer used, instead a typedef name appears before the struct declaration. Now the word struct is no longer required in variable declarations, making the code cleaner and easier to read.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE C MAIN COURSE PAGE. 

C MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT