structure in c

Category: Education

Presentation Description

Structure in C


By: ketan52262 (14 month(s) ago)

good 1

By: trizfarizzsha (19 month(s) ago)


Presentation Transcript

Structure in C:

Structure in C

What is Structure:

What is Structure Structure is a user define data type . It is group of different types of data variables. Structure constitute a sort of super data type. The C keyword struct declares a c structure . Tag is an optional name of a structure type .

Structure Over Array:

Structure Over Array Arrays’s disadvantage is that all the elements stored in an array are to be of the same data type. If we need to use a collection of different data type items it is not possible using an array. we require using a collection of different data items of different data types we can use a structure.

Slide 4:

Structure is a method of packing data of different types. A structure is a convenient method of handling a group of related data items of different data types.

Structure Declaration:

struct tag_name { data type member1; data type member2; … … } ; Example: struct lib_books { char title[20]; char author[15]; int pages; float price; }; Structure Declaration

Structure Declaration(Cont.):

Structure Declaration ( Cont .) The keyword struct declares a structure to holds the details of four fields namely title, author pages and price. These are members of the structures. Each member may belong to different or same data type. The structure we just declared is not a variable by itself but a template for the structure. We can declare structure variables using the tag name any where in the program. For example the statement, struct lib_books book1,book2,book3;

Structure Declaration(Cont.):

Structure Declaration ( Cont .) The complete structure declaration might look like this struct lib_books { char title[20]; char author[15]; int pages; float price; }; struct lib_books, book1, book2, book3;

Initialization of Structure:

Initialization of Structure Cannot initialize members in the structure declaration, because no memory has been allocated yet struct Student // Illegal { // initialization int studentID = 1145; string name = "Alex"; short year = 1; float gpa = 2.95; };

Initialization of Structure(cont.):

Initialization of Structure(cont.) Structure members are initialized at the time a structure variable is created An initialization list is an ordered set of values, separated by commas and contained in { } , that provides initial values for a set of data members {12, 6, 3} // initialization list // with 3 values

Example of Initializing Structure:

Example of Initializing Structure Structure Declaration Structure Variable struct Dimensions { int length , width, height; }; Dimensions box = {12,6,3}; box length 12 width 6 height 3

Giving values to member:

Giving values to member The members themselves are not variables they should be linked to structure variables in order to make them meaningful members. Link between a member and a variable is established using the member operator ‘.’ Which is known as dot operator or period operator. Book1.price Above is the variable representing the price of book1 and can be treated like any other ordinary variable. We can use scanf statement to assign values like scanf (“%s”,book1.file); scanf (“%d”,& book1.pages);

Simple Program of Structure in C:

Simple Program of Structure in C #include < stdio.h > #include < conio.h > struct student { int id; char *name; float percentage; } student1, student2, student3; int main() { struct student st ;; = "Angelina"; student3.percentage = 90.5; printf (" Id is: %d \n",; printf (" Name is: %s \n",; printf (" Percentage is: %f \n", student3.percentage); getch (); return 0; }

Nested Structure:

Nested Structure struct date { int day; int month; int year; }; struct student { int id_no ; char name[20]; char address[20]; char combination[3]; int age; structure date def; structure date doa ; } oldstudent , newstudent ;

Pointers to Structure:

Pointers to Structure When you use a pointer to a structure you must use -> instead of a dot. #include< stdio.h > typedef struct person { char *name; int age; } PERSON;

Pointers to Structure(cont.):

Pointers to Structure(cont.) int main() { PERSON p; PERSON * pptr ; PERSON pptr = &p; pptr ->name = "John Smith"; pptr ->age = 25; printf ("% s",pptr ->name); printf ("% d",pptr ->age); return 0; }