0% found this document useful (0 votes)
143 views51 pages

Structure

The document discusses various aspects of data structures in C, focusing on structures. It defines what a structure is and how it differs from arrays. It explains how to declare and define structures, access structure members, initialize and copy structures, use arrays of structures, and pass structures to functions. Structures can also contain arrays and other structures, and pointers can be used to access structure members.

Uploaded by

GurjarPushpa
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
143 views51 pages

Structure

The document discusses various aspects of data structures in C, focusing on structures. It defines what a structure is and how it differs from arrays. It explains how to declare and define structures, access structure members, initialize and copy structures, use arrays of structures, and pass structures to functions. Structures can also contain arrays and other structures, and pointers can be used to access structure members.

Uploaded by

GurjarPushpa
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 51

Contents

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; };

Tag name of structure

Structure Elements

Structure Variable Declaration

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:

typedef struct { . . . } type_name;

Structure members

type_name : used to declare structure variable as shown below

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.

Accessing Structure Members

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.

Three ways to access members


e.g.

typedef struct person_detail { char name[25]; int age; }person; person p1,*p; p=&p1;

Pointer, p assigned the address of structure variable,p1

According above example, there are three ways to access a structure member. . . Using dot notation : p1.age (*p).age p->age

Using indirection notation : Using selection notation :

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}; .... .... }

Assignment will be stud1.weight=60 stud1.height=180.75 An alternative to this is as follows struct student { . . };

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.

Rules for initializing structure


Initialization cannot be done within the structure body. The order of values enclosed in braces must match the order of members in the body. Partial initialization: initializing only the first few members and leave the remaining blank. The uninitialized members should be only at the end of the list.

The uninitialized members will be assigned default values as follows


Zero for integer and floating point numbers. \0 for characters and strings.

Copying and Comparing Structure 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

Arrays within Structures

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

Structures within Structures

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;

Structures and Functions

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.

Structures and Pointers

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

We could also use the following notation (*p).name

You might also like