Structures

Introduction

Structures are used when you want to process data of multiple data types but you still want to refer to the data as a single entity. Structures are similar to records in Cobal or Pascal. For example, you might want to process information on students in the categories of name and marks (grade percentages). Here you can declare the structure ‘student’ with the fields ‘name’ and ‘marks’, and you can assign them appropriate data types. These fields are called members of the structure. A member of the structure is referred to in the form of structurename.membername.

Program

struct student \ A
{
 char name[30]; \ B
 float marks; \ C
} student1, student2; \ D

main ( )
{
 struct student student3; \ E
 char s1[30]; \ F
 float f; \ G
 scanf ("%s", name); \ H
 scanf (" %f", & f); \ I
 student1.name = s1; \ J
 student2.marks = f; \ K
 printf (" Name is %s 
", student1.name); \ L
 printf (" Marks are %f 
", student2.marks); \ M
}

Explanation

  1. Statement A defines the structure type student. It has two members: name and marks.
  2. Statement B defines the structure member name of the type character 30.
  3. Statement C defines the structure member marks of the type float.
  4. Statement D defines two structure variables: structure1 and structure2. In the program you have to use variables only. Thus struct student is the data type, just as int and student1 is the variable.
  5. You can define another variable, student3, by using the notations as specified in statement E.
  6. You can define two local variables by using statements F and G.
  7. Statement J assigns s1 to a member of the structure. The structure member is referred to as structure variablename.membername. The member student1.name is just like an ordinary string, so all the operations on the string are allowed. Similarly, statement J assigns a value to student1.marks
  8. Statement L prints the marks of student1 just as an ordinary string.

Points to Remember

  1. Structures are used when you want to process data that may be of multiple data types.
  2. Structures have members in the form:

    structurename.membername.
    

COMPLEX STRUCTURE DEFINITIONS

Introduction

You can define structures of arrays or arrays of structures, etc. The following section gives definitions of complex structures.

Program


Struct address \ A
{
 plot char [30], struc char[30];
 city char[30]
}
struct student \ B
{
 name char[30];
 marks float;
 struct address adr; \ C
}
main ( )
{
 struct student student1; \ D
 struct student class[20]; \ E
 class[1].marks = 70; \ F
 class[1].name = " Anil ";
 class[1].adr.plot = "7 "; \ G
 class[1].adr.street = " Mg Road";
 class[1].adr.city = "mumbai";

 printf( " Marks are %d
", class[1].marks);
 printf( " name are %s
", class[1].name);
 printf( " adr.plot is %s
", class[1].adr.plot);
 printf( " adr.street is %s
", class[1].adr.stret);
 printf( " adr.city is %s
", class[1].adr.city);
}

Explanation

  1. Statement A declares the address of a structure containing the members plot, street and city.
  2. Statement B declares a structure having 3 members: name, marks, and adr. The data type of adr is structure address, which is given by statement C.
  3. Statement D defines the variable student1 of the data type struct student.
  4. Statement E defines an array class with 20 elements. Each element is a structure.
  5. You can refer to marks of the students of class[1] using the notation class[1].marks. class[1] indicates the first element of the array, and since each element is a structure, a member can be accessed using dot notation.
  6. You can refer to the plot of a student of class[1] using the notation class[1].adr.plot. Since the third element of the structure is adr, and plot is a member of adr, you can refer to members of the nested structures.
  7. If you want to refer to the first character of the character array plot, then you can refer it as

    Class[1].adr.plot[0]
    

    because plot is a character array.

Points to Remember

  1. When a structure is a member of another structure it is called a nested structure.
  2. You can define structures of arrays or arrays of structures, and the members are referred to using dot notations.

MEMORY ALLOCATION TO STRUCTURE

Introduction

For each structure, variable memory is allocated. The following sections give the memory layout of the structure student1.

Program/Example

student1
 student1 0 name
30 marks
34 adr plot
64 street
94 city

Explanation

  1. Suppose the base address of the allocations is 0; then the first member name starts from 0.
  2. Since name has 30 characters, the second member, marks, starts from location 30; marks occupies 4 bytes.
  3. The third member, adr, starts from location 34, so the first member of adr starts from location 34. Period plot occupies 30 bytes, so street starts at 64.
  4. city starts at 94.
  5. You can print the addresses of the members using the following printf statements:

    printf( "16lu
    ", &student1.marks);
    printf( "16lu
    ", &student1.adr.plot);
    

Point to Remember

The structure members are allocated consecutive memory locations.

PROGRAMMING WITH STRUCTURES

Introduction

You can write programs with structures by using modular programming.

Program

struct student
{
 name char[30];
 marks float;
}
main ( )
{
 struct student student1;
 student1 = read_student ( )
 print_student( student1);
 read_student_p(student1);
 print_student (student1);
}
struct student read_student( ) \ A
{
 struct student student2;
 gets(student2.name);
 scanf("%d",&student2.marks);
 return (student2);
}
void print_student (struct student student2) \ B
{
 printf( "name is %s
", student2.name);
 printf( "marks are%d
", student2.marks);
}
void read_student_p(struct student student2) \ C
{
 gets(student2.name);
 scanf("%d",&student2.marks);
}

Explanation

  1. The function read_student reads values in structures and returns the structure.
  2. The function print_student takes the structure variable as input and prints the content in the structure.
  3. The function read_student_p reads the data in the structure similarly to read_student. It takes the structure student as an argument and puts the data in the structure. Since the data of a member of the structure is modified, you need not pass the structure as a pointer even though structure members are modified. Here you are not modifying the structure, but you are modifying the structure members through the structure.

Points to Remember

  1. You can write a function that returns the structure. While writing the function, you should indicate the type of structure that is returned by the function. The return statement should return the structure using a variable.
  2. You can pass a structure as an argument. You can modify a member of the structure by passing the structure of an argument. The changes in the member made by the function are retained in the called module. This is not against the principle of call by value because you are not modifying the structure variable, but are instead modifying the members of the structure.

STRUCTURE POINTERS

Introduction

You can process the structure using a structure pointer.

Program

struct student \ A
{
 char name[30]; \ B
 float marks; \ C
}; \ D

main ( )
{
 struct student *student1; \ E
 struct student student2; \ F
 char s1[30];
 float f;
 student1 = &student2; \ G
 scanf ("%s", name); \ H
 scanf (" %f", & f); \ I
 *student1.name = s1; \ J student1-> name = f;
 *student2.marks = f; \ K student1-> marks = s1;

 printf (" Name is %s 
", *student1.name); \ L
 printf (" Marks are %f 
", *student2.marks); \ M
}

Explanation

  1. Statement E indicates that student1 is the pointer to the structure.
  2. Statement F defines the structure variable student2 so that memory is allocated to the structure.
  3. Statement G assigns the address of the structure student2 to the pointer variable structure student1.
  4. In the absence of statement G, you cannot refer to the structure using a pointer. This is because when you define the pointer to the structure, the memory allocation is done only for pointers; the memory is not allocated for structure. That is the reason you have to declare a variable of the structure type so that memory is allocated to the structure and the address of the variable is given to the point.
  5. Statement J modifies a member of the structure using the * notation. The alternative notation is

    student1-> name = f;
    student1-> marks = s1;
    

Points to Remember

  1. You can access members of the structure using a pointer.
  2. To access members of the structure, you have to first create a structure so that the address of the structure is assigned to the pointer.


C & Data Structures
C & Data Structures (Charles River Media Computer Engineering)
ISBN: 1584503386
EAN: 2147483647
Year: 2006
Pages: 232

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net