Top Banner
March 25, 2022 1 (B.V.Patel institute of STRUCTURE AND UNIONS
23

21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Dec 24, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

April 19, 2023 1

(B.V.Patel institute of BMC & IT)

STRUCTURE AND

UNIONS

Page 2: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

April 19, 2023

2

Defining a structure

Struct book_bank

{

char title [20];

char author[15];

int pages;

float price;

};

A structure is convenient tool for handling a group of logically related different data items. for ex.customer(address,id,name)

Page 3: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Declare structure variable Struct book_bank { char title [20]; char author[15]; int pages; float price; } book_bank book1;

Page 4: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Accessing structure members The link between member and variable

is establish using dot operator For ex.book1.pages=250; Another method for ex. Strcpy(book1.author,”balagurusamy”); Another method we cal also scanf to values scanf(“%d\n”,&book1.pages);

Page 5: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Structure member initialization Compile time main() { struct student { char name[20]; int marks; }stud; stud. marks={50}; …………… }

Page 6: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Rum time

Scanf (“%d \n ”,&stud. marks);

Page 7: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

method of initialize structure Structure can be initialize within main or

outside the main

main() { struct student { char name[20]; int marks; }stud; stud. marks={50}; …………… }

struct student { char name[20]; int marks; }stud; Main() { stud. marks={50);

}

Page 8: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Operations on individual member

If(stud1.number == 111) { float

sum=stud1.marks+stud2.marks; stud2.marks *=0.5; }

Page 9: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Array of structure Struct marks { int sub1; int sub2; int sub3; }marks student main() { Struct marks

student[3]={{45,34,61}, {23,55,67},{44,66,55}}; }

Page 10: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Array initializes members like

student[0].subject1=45; student[0].subject2=34; ………………… student[2].subject3= 55 ;

Page 11: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Arrays within structure Struct marks { int number; float subject[3]; }student[2]; here the member subject contains

three elements subejct[0], subejct[1], subejct[2].

these elements can be accessed using appropriate subscript like student[1].subject[2] refer marks obtain in third subject by second student.

Page 12: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Structure within structure Struct salary { char name; char dept; Struct { int houserent; int da; } allowance;}Employee; Now for refer member we can use

employee.allowance.houserent

Page 13: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

union Union item { int m; float x; char c; }code; code.m=379;

Page 14: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

union It is like structure but main difference is

in term of storage

1000 1001 1002 1003

C

M

X

Page 15: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

ENUM is closely related to the #define preprocessor.

It allows you to define a list of aliases which represent integer numbers. For example if you find yourself coding something like:

#define MON 1 #define TUE 2 #define WED 3 You could use enum as below. enum week { Mon=1, Tue, Wed, Thu, Fri

Sat, Sun} days; or enum boolean { FALSE = 0, TRUE };

April 19, 2023 15

Page 16: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

int main() { /* * Define a list of aliases */ enum months {Jan=1, Feb, Mar, Apr,

May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; enum months month; /* define 'month'

variable of type 'months‘ */ printf("%d\n", month=Sep); /* Assign integer value via an alias * This

will return a 9 */ }

April 19, 2023 16

Page 17: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

April 19, 2023 17

int main() { /* * Define a list of aliases */ enum days {Jan=31, Feb=28, Mar=31,

Apr=30, May=31, Jun=30, Jul=31, Aug=31, Sep=30, Oct=31, Nov=30, Dec=31};

printf("%d\n", month=Feb); /* Assign integer value via an alias * This will return 28 */

}

Page 18: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

It is a program that processes our source program before it is passed to the complier.

The preprocessor offers several features called preprocessor directives. Each of these preprocessor directives begins with # symbol. The directives can be placed anywhere in the program but most often placed at the beginning of program. There are four types of preprocessor directives as follow.

  Macro expansion File inclusion Conditional compilation Miscellaneous directives

April 19, 2023 18

Page 19: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

#define PI 3.1415

The above statement is called macro definition or macro. During preprocessing the processor replaces every occurrence of PI in program with 3.1415. It is also called macro templates whereas 3.1415 are called macro expansions

April 19, 2023 19

Page 20: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

This preprocessor directive causes one file to be included in another. The preprocessor command for file inclusion looks like this:

#include “filename” and it simply easy causes the entire

contents of filename to be inserted into the source code at that point in the program

April 19, 2023 20

Page 21: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

if we want, have the compiler skip over part of a source code by inserting the preprocessing commands #ifdef and #endif, which have the general form:

#ifdef macronamestatement 1;statement 2;statement 3;

#endif  If macroname has been #defined, the

block will be processed as usual; otherwise not

April 19, 2023 21

Page 22: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

There are two more preprocessor directives available, though they are not very commonly used. They are:

  #undef #pragma

April 19, 2023 22

Page 23: 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

23