18 – Structures in C Programming Language

Structures

Arrays allow to define types of variables that can hold several data items of the same kind similarly structure is another user defined data type in C that allows to combine data items of different kinds.

Structures are used to represent a record. Suppose if you want to keep track of your books in a library, you might want to track the following attributes about each book:

  • Title
  • Author
  • Subject
  • Book ID

Defining Structure

To define a structure, you have to use the struct statement. The struct statement defines a new data type, with more than one number.
The format of the struct statement is as follows:

The structure tag is optional & each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure’s definition, before the final semicolon, you can specify one or more structures variables but it is optional. Here is the way you would declare the Book structure.

Accessing structure Members

We use the member access operator (.), to access any member of a structure. The member access operator coded as a period between the structure variable name & the structure member. You would use the keyword struct to define variables of structure type. The following program shows the use of a structure in a program:

Structures as Function Arguments

We can pass a Structure as a function arguments in the same way as you pass any other variable or pointer.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* function declaration */
void printBook( struct Books book );

int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali");
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info */
   printBook( Book1 );

   /* Print Book2 info */
   printBook( Book2 );

   return 0;
}

void printBook( struct Books book ) {

   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
}

Pointer to Structures

In the same way, you can define pointers to the Structures as you define pointer to any other variable:

Now we will re-write the above example using structure pointer:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* function declaration */
void printBook( struct Books *book );
int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali");
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info by passing address of Book1 */
   printBook( &Book1 );

   /* print Book2 info by passing address of Book2 */
   printBook( &Book2 );

   return 0;
}

void printBook( struct Books *book ) {

   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);

Bit Fields

Bit fields allow the packaging of data in a Structure. This is useful when memory or data storage is at a premium. Examples include:

  • Packing objects into a machine code. e.g. 1 bit flags can be compacted.
  • Reading external file formats – non standard files could be read in , e.g. 9 bit integers.

C allow us to do this in a Structure definition by putting: bit length after the variable.
For Example:


1
2
3
4
5
6
7
8
struct packed_struct {
   unsigned int f1:1;
   unsigned int f2:1;
   unsigned int f3:1;
   unsigned int f4:1;
   unsigned int type:4;
   unsigned int my_int:9;
} pack;

Here, the packed_struct contains 6 members:

C automatically packs the above bit fields as compactly as possible, provided that the maximum length of the field is less than or equal to the integer word length of the computer. If this is not the case, then some compilers may allow memory overlap for the fields while others would store the next field in the next word.

Subscribe our channels:

Click here for more Articles:

10 – Decision Making or Selection Statements In C Language

Selection Statements

In this chapter, we will learn Selection Statements.

Decision Making

Decision making structure requires that the programmer specifies one or more conditions to be evaluated or tested by the program along with statements to be executed, if the condition is determined to be true & Optionally, if the condition is false other statements will be executed.

Read more10 – Decision Making or Selection Statements In C Language