Structure
Structure
Data Structure Array Vs Structure Structure Definition Structure Variable Declaration Accessing Structure Members Structure Initialization Copying and Comparing Structure Variables
Contents cont
Arrays of Structures Arrays within Structures Structures within Structures Structures and Functions Structures and Pointers
Data Structure
Data Structure is a way of organizing data that consider not only the item stored but also their relationship to each other. Data Structure makes it possible to manage huge amounts of data, for example x Large Databases x Internet Indexing Services
Example of Data Structure : Array Structure Stack Queue Link List Tree Graph etc
Array Vs Structure
Both Arrays and Structures are classified as data structure as they provide a mechanism that enable us to access and manipulate data in a relatively easy manner. But they differ in a number of ways
ARRAY
Collection of related data elements of same type Derived data type Behaves like builtin data type, just to declare a variable
STRUCTURE
Related elements of different data types Programmerdefined data type Design & declare a structure before variable of that type are declared & used
Structure Definition
Syntax:
struct tag_name { datatype member1; datatype member2; . . . datatype memberN; };
Where
struct : keyword tag_name : any valid identifier datatype : valid data type member : any valid variable name/identifier members are also known as structure elements The structure body must terminated with a semicolon ; .
Example:
struct book_bank { char title[20]; char author[20]; int pages; float price; };
Structure Elements
Syntax:
Two ways to declare a structure variable
Along with structure definition
x struct tag_name { datatype member1 ------------}var1,var2. . . varN;
Structure Variables
Example:
struct book_bank
{ . . . }book1,book2,book3;
Structure variables
Separately
x struct tag_name var1,var2varN;
Structure variable
Example
x struct book_bank book1,book2,book3;
Points to be noted
Members of structure are not variable. They do not occupy any space in memory until they are associated with the structure variable such as book1 etc.
TypeType-defined structures
Keyword typedef is use to define a structure as follows:
Structure members
type_name var1,var2;
Remember
type_name : type definition name, not a variable. We cant define a variable with typedef declaration.
Size of Structure
Size of structure is equals to the number of bytes required to hold all the members of the structure. E.g. struct point {
int x; int y; }p1;
Size of structure point = sizeof(x)+sizeof(y) =2+2 =4 bytes we can also use expression
sizeof(struct p1); sizeof(exp/type) returns size, in byte, of expression/type.
Member Operator
The link between a member and a variable is established using the member operator . which is known as Dot operator or Period operator. For e.g.
book1.price;
is variable representing the price of book1 and can be treated as any other ordinary variable.
How we can assign value to member strcpy(book1.title,Programming in ANSI C); book1.pages=493; book1.price=200.00; We can also use scanf to give the values scanf(%s\n,book1.title) ; scanf(%d,&book1.pages); are valid input statements.
typedef struct person_detail { char name[25]; int age; }person; person p1,*p; p=&p1;
According above example, there are three ways to access a structure member. . . Using dot notation : p1.age (*p).age p->age
Structure Initialization
A structure variable can be initialized at compile time. For e.g. main() { struct student { int weight; float height; }stud1={60,180.75}; .... .... }
main() { struct student stud1={60,180.75}; .... .... } Initialization cannot within the structure body. It must be done only in the declaration of the actual variables.
Two variables of the same structure type can be copied the same way as ordinary variable. If person1 & person2 belong to the same structure, then the following statements are valid : x person1=person2; x person2=person1; Some invalid statements are : person1==person2; person1!=person2
Arrays of Structures
Syntax :
struct tag_name var_arr[size]; Where x tag_name : name of structure. x var_arr : an array which consist of size number of elements of type structure. x size : number of elements of type structure.
Example struct class student[100]; defines an array called student, that consists of 100 elements.
struct marks { int s1; int s2; int s2; }; main() { struct marks student[3]= {{45,68,81},{75,53,69},{57,39,85}}; . }
According to pervious declaration, initialization of member will be as follows : student[0].s1=45; student[0].s2=68; . . . student[2].s3=85; example
C permits the use of array as structure member. we can use both single-or multidimensional array. For e.g. struct marks Array within { structure int num; float sub[3]; }stud[3]; It can be access as follows Marks obtain by first student in stud[1].sub[2]; second subject
A structure can be nested inside another structure. Following code segment illustrates it: struct addr { int houseNo; char area[25]; char city[15]; char state[25]; };
struct emp { int empno; char name[26]; char desig[25]; addr address; float basic; }; emp worker;
Here address itself is a structure variable of type addr and member of another structure emp
Structure Variable
worker.address.houseno=1234;
Passing structures to functions structures can be passed to functions by value as well as by reference just like other variables. In Call by value, the called function copies the passed structure on to its own work copy; the original structure then remains unaffected. In Call by Reference, the called function declares a reference for the passed structure and refers to the original structure elements through its reference.
name of an array of structure variables represents the address of its zeroth element. For example : struct inventory { char name[30]; int number; float price; }product[2],*p;
The assignment
p=product; would assign the address of zeroth element of product to p. that is, the pointer p will point to product[0]; Members of structure can be accessed by using arrow operator,(->).such as p->name p->price p->number