17 structure-and-union

25
Structure

Upload: rohit-shrivastava

Post on 15-Apr-2017

148 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: 17 structure-and-union

Structure

Page 2: 17 structure-and-union

Basic Idea• Ordinary variables can hold one piece of information and arrays

can hold a number of pieces of information of the same data type.

• These two data types can handle a great variety of situations.• But quite often we deal with entities that are collection of

dissimilar data types.• For example, suppose you want to store data about a book. You

might want to store its name (a string), its price (a float) and number of pages in it (an int).

• If data about say 3 such books is to be stored, then we can follow two approaches:

• (a) Construct individual arrays, one for storing names, storing prices and still another for storing number of pages.

• (b) Use a structure variable.

Page 3: 17 structure-and-union

Introduction• A structure is a user defined data type which keeps variables

of different data types under a single name, unlike array which keeps only single data type values.

Page 4: 17 structure-and-union

Cont…• The program becomes more difficult to handle as the number

of items relating to the book go on increasing.

Page 5: 17 structure-and-union

Defining a Structure

Page 6: 17 structure-and-union
Page 7: 17 structure-and-union
Page 8: 17 structure-and-union
Page 9: 17 structure-and-union
Page 10: 17 structure-and-union

Memory for Structure

• Memory is allocated to structure during creation of variable of structure not during declaration of structure.

• Continuous memory is allocated to structure starts from first element of structure

• Ex.• struct detail{

int a;char b;int c[10];

}d1;• Memory Map;

Page 11: 17 structure-and-union
Page 12: 17 structure-and-union
Page 13: 17 structure-and-union
Page 14: 17 structure-and-union
Page 15: 17 structure-and-union
Page 16: 17 structure-and-union
Page 17: 17 structure-and-union

typedef : An exampletypedef struct{

float real;float imag;

} _COMPLEX;

_ COMPLEX a,b,c;

Syntax:typedef existing_name new_nameExample:typedef unsigned int uint;uint a,b; //a,b are of type unsigned int

Page 18: 17 structure-and-union
Page 19: 17 structure-and-union
Page 20: 17 structure-and-union

Exampletypedef struct{float real;float imag;} _COMPLEX;

void swap (_COMPLEX a, _COMPLEX b){

_COMPLEX tmp;tmp = a;a = b;b = tmp;

} void print (_COMPLEX a){printf("(%f, %f) \n",a.real,a.imag);}

void main(){_COMPLEX x={4.0,5.0}, y={10.0,15.0};print(x); print(y);swap(x,y);print(x); print(y);}

Page 21: 17 structure-and-union

Union• Union is user defined data type used to store data of dissimilar

types under unique variable name at single memory location.• Syntax of union is similar to structure.• The major difference between structure and union is storage. In

structures, each member has its own storage location, whereas all the members of union use the same location.

• It can handle only one member at a time.• Union holds value for one data type which requires larger storage

among their members.

Page 22: 17 structure-and-union

Union Example#include <stdio.h>union Engg{

int id;char name[20];

}Eg;void main(){

printf("Enter developer id : ");scanf("%d", &Eg.id);printf("\n Enter developer name : ");scanf("%s", Eg.name);printf("\n Developer ID : %d", Eg.id); //Garbage Valueprintf("\n Developed By : %s", Eg.name);

}

Page 23: 17 structure-and-union

Enum• Enum stands for enumerated data type.• It is user defined data type whose elements are

treated as constant.• The enumerator names are usually identifiers

that behave as constants in the• language.• First Element of enumerator starts with 0,

second element 1,third element 2 and• so on. We can also change the value of elements

to our desired choice.

Page 24: 17 structure-and-union

Enum Example#include <stdio.h>enum {sun,mon,tue,wed,thu,fri,sat};enum {f,t};enum {zero=1,one,two=5,three};void main(){

int i;for(i=sun;i<=sat;i++)

printf("Code for day is : %d \n",i);if(t==1)

printf("Enum is Awesome \n");printf("%d ",zero);printf("%d ",one);printf("%d ",two);printf("%d ",three);}

Page 25: 17 structure-and-union

Program for atoi()int myAtoi(char *str){ int res = 0; // Initialize result for (int i = 0; str[i] != '\0'; ++i) res = res*10 + str[i] - '0'; return res; // return result.}int main(){ char str[] = "9789"; int val = myAtoi(str); printf ("%d ", val); return 0;}

Output:9789