Docsity

C Structures: Declaration, Access, Initialization, and Arrays, Slides of Computer Science

Partial preview of the text

Download C Structures: Declaration, Access, Initialization, and Arrays and more Slides Computer Science in PDF only on Docsity!

STRUCTURES

1

Contents

  • Overview
  • Structure and their variables declaring
  • Accessing the members of a Structure
  • Initialization of Structures
  • Copying and Comparing Structures
  • Nesting of Structures
  • Arrays of Structures
  • Structures and Pointers
  • Structures and Functions (^2)

Structure and their variables declaring

  • Structure is declared by using the keyword called struct
  • The struct is followed by the body of the structure
  • Within the body of structure we declares variables or members of the structure
  • The format is as follows: struct { <data type member_name 1 >; <data type member_name 2 >; ….. ; } <structure_variable 1 >,<structure_variable 2 >,…;
  • In the above format structuretagname is optional and not needed if the structure variables are defined
  • The body of structure contain any number of variables separated by semicolons
  • Whereas at the end structure_variables define the actual names of the individual structures 4

Structure and their variables declaring

  • structure_variables can be defined separately by specifying the following declaration statement:
  • struct <structure_variable>;
  • This creates the structure_variables and has a separate instance of all the member_names in structure tag name.
  • For example 1 :
  • Declaration of structures and definition of structure variables. OR
  • The above statement is defining the variables of character type called name , integer type called roll_no , and character type called address 5

struct students

char name[ 30 ];

int roll_no[ 6 ];

char address[ 30 ];

}abc, xyz;

struct students

char name[ 30 ];

int roll_no[ 6 ];

char address[ 30 ];

struct students abc, xyz;

Accessing the members of a Structure

  • The members of a structure can be accessed using the ‘.’, which is called ‘dot operator’.
  • The members are accessed by relating them to the structure variable with a dot operator
  • The general form of the statement for accessing a member of a structure is as follows:
  • <structure_variable>.<member_name>;
  • For example 3 : struct abc { int a; int b; int c; }ab, bc; The first member can be accessed by the construct ab.a; 7

Accessing the members of a Structure

  • We can also assign a value to the member of the structure
  • Suppose we want to access the value 100 , say to member c identified by the variable bc , thus the statement can be written as:
  • bc.c = 100 ;
  • To print this value assigned to the member on the screen, the following code is written:
  • printf(“%d”, bc.c);
  • To input the value of c , the following code can be used:
  • scanf(“%d”, bc.c);
  • Now consider the another structure which are as follows: struct personal_data { int rollno[ 6 ]; char name[ 30 ]; char address[ 30 ]; }; (^8)

Initialization of Structures

  • A structure can be initialized in much the same way as any other data type
  • This consists of assigning some constants to the member of the structure
  • Structures that are not explicitly initialized by the programmer are, by default, initialized by the system
  • The general construct for initializing a structure can be any of the two forms given as follows: struct <structure_tag_name> { <data_type member_name 1 >; <data_type member_name 2 >; …. }<structure_variable> = {constant 1 , constant 2 ,.. .}; OR struct<structure_tag_name><structure_variable> = {constant 1 , …}; 10

Initialization of Structures

  • The following are some examples using both the forms of initialization:
  • For example 4 :
  • Initialization of structure using the first construct #include <stdio.h> struct medicine { int count; int m_date, m_month, m_year; int ex_date, ex_month, ex_year; }batch={ 1000 , 10 , 04 , 2018 , 20 , 09 , 2019 }; main() { printf(“\n count=%d”, batch.count); printf(“\n mfg-date=%d%d%d”, batch.m_date, batch.m_month, batch.m_year); printf(“\n exp-date=%d%d%d”, batch.ex_date, batch.ex_month, batch.ex_year);} 11

Copying and Comparing Structures

  • A structure can be assigned to another structure of the same type
  • For example 6 :
  • Copying one structure to another of the same type #include<stdio.h> struct employee{ char grade; int basic; float allowance; }; main() { struct employee abc={‘a’, 7000 , 650. 50 }; struct employee xyz; xyz=abc; printf(“\n The grade of xyz ix %c, basic is Rs %d, allowance is Rs %f”, xyz.grade, xyz.basic, xyz.allowance); return 0 ;} 13

Copying and Comparing Structures

  • Comparing one structure variable with another is not allowed in C.
  • However, members of one structure can be compared with members of another on an individual basis
  • In fact, the members involved in the comparison will behave like any other variable
  • For example 7 :
  • Comparison of individual members of structures #include<stdio.h> struct employee{ char grade; int basic; float allowance; }; main() { struct employee abc={‘a’, 7000 , 650. 50 }; (^14)

Nesting of Structures

  • A structure can be placed within another structure
  • In other words, structures can contain other structures as members
  • A structure within a structure means nesting of structures
  • In such cases, the dot operator in conjunction with the structure variables are used to access the members of the innermost as well as outermost structures.
  • For example 8 :
  • A C program to demonstrate nesting of structures and accessing structure members #include<stdio.h> struct outer {int out 1 ; float out 2 ; struct inner {int in 1 ; float in 2 ;}invar; }; (^16)

Nesting of Structures

main() { struct outer outvar; outvar.out 1 = 2 ; outvar.out 2 = 20. 50 ; outvar.invar.in 1 = 3 ; outvar.invar.in 2 = 30. 50 ; printf(“out 1 =%d, out 2 =% 6. 2 f, in 1 =%d, in 2 =% 6. 2 f”, outvar.out 1 , outvar.out 2 , outvar.invar.in 1 , outvar.invar.in 2 ); return 0 ; } 17

Arrays of Structures

struct demo {char a; int i; float f; }d[ 3 ]; main() { int n; for(n= 0 ;n<= 2 ; ++n) { printf(“\nEnter ch, in, f 1 :”); scanf(“%c %d %f”, &(d[n].a), &(d[n].i), &(d[n].f); printf(“\n a=%c, i=%d, u=%f”, d[n].a, d[n].i, d[n].f); } return 0 ; } 19

Structures and Pointers

  • A pointer to a structure is not itself a structure, but merely a variable that holds the address of the structure
  • Declaring pointers to a structure is basically the same as declaring a normal pointer
  • Syntax: struct <structure_tag_name> { <datatype n 1 >; <datatype n 2 >; …. ; }*ptr; OR struct <structure_tag_name> *ptr;
  • This pointer *ptr can be assigned to any other pointer of the same type
  • It can be used to access the members of its structure (^20)