1 of 20

CSO101: Computer Programming

Structures 1

O.S.L. Bhavana

2 of 20

Composite Data Types

  • How to store composite Name, Roll No, Marks, Section Name

of 158 students?

  • char name[158][40];
  • int rollNo[158];
  • float marks[158];
  • char section[158];

Maintaining many arrays and keeping track of their indices is combursome

3 of 20

Structures

  • A structure is a user-defined data type
  • A structure contains a number of different data types together

4 of 20

Structures : Definition

  • struct is a keyword;
  • student is the name we gave to this structure
  • In this example, the structure contains 4 variables of different data types
  • Each variable in the structure is called a field of the structure
  • The definition of structure must end with a semicolon

5 of 20

Structures : Declaration of variables

  • Syntax: struct structure_name variable_name;
  • All variable naming rules apply for struct variables too.
  • Declaration must come after definition of the structure
  • Declaration along with definition is also possible.
  • Declaration of a struct variable allocates space for all field of the structure in contiguous memory locations
  • Ex2: on board

6 of 20

Structures: Memory Map

  • Taught on board

7 of 20

Structure: Accessing Fields of a struct

  • ‘ . ‘ operator is used to access fields of a structure
  • a.marks refers to the marks field in the struct variable a
  • a.marks can be treated as any float variable
  • b.name refers to the character array name in the struct variable b
  • b.name may be treated as any character array

8 of 20

Structures: Initialization (with list)

  • List initialization can happen only in combination with declaration
  • List initialization should not be separated from declaration
  • In case of partial list initialization the uninitialized fields take value zero
  • The whole struct variable may also be initialized to 0

9 of 20

Structures: Initialization after declaration

  • After declaration, list initialization cannot be done and each field of the structure has to be initialized separately
  • A uninitialized field may have garbage values

10 of 20

Arrays of Structures

  • Array of a structure is a collection of structure variables of the same structure
  • Declaration: struct struture_name array_name [array_size];
      • Ex: struct student classBE[158];
  • Accessing Fields of an array element: array_name[array_index].fieldname
        • Ex: classBE[23].marks;
      • classBE[i] refers to the i+1th element in the array of students

11 of 20

Array of Structures

12 of 20

Memory Map of Array of Structures

  • Taught on board

13 of 20

Assignment of Structures

  • To copy the data of a structure variable into another variable of the same structure
    • Each field may be copied individually
    • The full structure may be copied at once

14 of 20

Structures: Nesting

  • Field of structure may itself be another structure

15 of 20

Structures: sizeof

  • Fields of a structure are stored in memory one after the other

in the order they are declared in the structure

  • But they are not always in memory locations next to each other

16 of 20

Structures: sizeof

  • In the code sizeof the structure is printed to be 8 and not 5
  • The address of x.b is 4bytes away from the address of x.a

17 of 20

Structures: Data Alignment

  • The memory is a sequence of bytes
  • The CPU accesses memory in the form of memory words. A word is collection of 4bytes or 8 bytes depending on the machine
  • The memory is actually organised as 4 (or 8) data banks. Suppose if a memory has 16bytes it can be organised in 4 data banks as follows

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

A word

Another word

18 of 20

Structures: Data Alignment

  • Since each word can be accessed in just one memory access, a 4-byte integer is usually stored at addresses that are multiples of 4
    • A integer stored in memory cells 8,9,10,11 can be accessed in one memory access by retreiving the word starting with address 8
    • If an integer is stored in memory cells 6,7,8,9 it should be accessed by making 2 memory accesses – one to the word starting at 4 and one to the word starting at 8

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Memory cells 6,7,8,9

do not form a word

19 of 20

Structures: Data alignment

  • With the same reasoning a (built-in datatype) variable is stored at an address which is a multiple of the size of the data type
    • char is stored at an address that is a multiple of sizeof(char)
    • short int is stored at an address that is a multiple of sizeof(short int)
    • Similarly for other data types
  • In the previous example, if the char field is stored at address 4, the next field which is an integer (by default) will not be stored in cells 5 or 6 or 7, but it will be stored from the address 8.
  • The total size of the struct is turns out 8 bytes

20 of 20

Structures: Data Alignment

8 bytes is the size of the above struct.

First byte for char;

2nd byte left for alignment;

3,4 for short int

5-8 for int