12
and STRUCTURE 1

C programing -Structure

Embed Size (px)

Citation preview

Page 1: C programing -Structure

1

and STRUCTURE

Page 2: C programing -Structure

2

Contents:

Introduction Syntax and formation Variables and member accessing Nesting of structures Use of function and pointer in

structure

Page 3: C programing -Structure

3

Intro: Structure is another user defined data type like

array available in C , that allows to combine data items of different kinds of data types, whereas arrays allow only of same data type .

Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book − Title

− Author − Subject − Book ID

All these data can be saved under a single name in STRUCTURE

Page 4: C programing -Structure

4

Structure declarationsTo define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows −

struct [structure tag] {

member definition; member definition; ... member definition;} [one or more structure variables];

struct person{ char name[50]; int cit_no; float salary;};

We can create the structure for a person as mentioned above as:

Page 5: C programing -Structure

5

Structure variable declaration

When a structure is defined, it creates a user-defined type but, no storage is allocated. For the above structure of person, variable can be declared as:

struct person{ char name[50]; int cit_no; float salary;};

Inside main function:struct person p1, p2, p[20];

Another way of creating sturcture variable is:struct person

{ char name[50]; int cit_no; float salary;}p1 ,p2 ,p[20];

Page 6: C programing -Structure

6

Member accessAccessing members of a structureThere are two types of operators used for accessing members of a structure.

1. Member operator(.)2. Structure pointer operator(->)

Any member of a structure can be accessed as: structure_variable_name.member_name

Suppose, we want to access salary for variable p2. Then, it can be accessed as:

p2.salary

Page 7: C programing -Structure

7

Nested StructuresStructures can be nested within other structures in C programming.

struct complex{

int imag_value; float real_value;};struct number{

struct complex c1; int real;

}n1,n2;

Suppose you want to access imag_value for n2 structure variable then, structure member n1.c1.imag_value is used.

Page 8: C programing -Structure

CS 3090: Safety Critical Programming in C 8

Sample program

Page 9: C programing -Structure

9

Pointers to Structure :We can define pointers to structures in the same way as we define pointer to any other variable −

struct Books *struct_pointer;

Now, we can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the '&'; operator before the structure's name as follows −

struct_pointer = &Book1;

To access the members of a structure using a pointer to that structure, you must use the -> operator as follows −

struct_pointer->title;

Page 10: C programing -Structure

10

Example using function and pointer:#include <stdio.h>#include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id;};

/* function declaration */void printBook( struct Books *book );int main( ) {

struct Books Book1; /* Declare Book1 of type Book */ struct Books Book2; /* Declare Book2 of type Book */

/* book 1 specification */ strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407;

/* book 2 specification */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700;

Page 11: C programing -Structure

Example using function and pointer:

11

/* print Book1 info by passing address of Book1 */ printBook( &Book1 );

/* print Book2 info by passing address of Book2 */ printBook( &Book2 );

return 0;}

void printBook( struct Books *book ) {

printf( "Book title : %s\n", book->title); printf( "Book author : %s\n", book->author); printf( "Book subject : %s\n", book->subject); printf( "Book book_id : %d\n", book->book_id);}

Book title : C ProgrammingBook author : Nuha Ali

Book subject : C Programming TutorialBook book_id : 6495407

Book title : Telecom BillingBook author : Zara Ali

Book subject : Telecom Billing TutorialBook book_id : 6495700

OUTPUT

Page 12: C programing -Structure

12