17 structure-and-union
TRANSCRIPT
![Page 1: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/1.jpg)
Structure
![Page 2: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/5.jpg)
Defining a Structure
![Page 6: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/6.jpg)
![Page 7: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/7.jpg)
![Page 8: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/8.jpg)
![Page 9: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/9.jpg)
![Page 10: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/11.jpg)
![Page 12: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/12.jpg)
![Page 13: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/13.jpg)
![Page 14: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/14.jpg)
![Page 15: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/15.jpg)
![Page 16: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/16.jpg)
![Page 17: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/18.jpg)
![Page 19: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/19.jpg)
![Page 20: 17 structure-and-union](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022070520/58f1ae481a28abc2438b45f9/html5/thumbnails/25.jpg)
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