Pages

STRUCTURES UNIT 15 PART 2


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\n", student2.name);
    printf( "marks are%d\n", 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 \n", *student1.name);    \\ L
    printf (" Marks are %f \n", *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
6.  student1-> name = f;
7.  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.