17 structure-and-union
TRANSCRIPT
Structure
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.
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.
Cont…• The program becomes more difficult to handle as the number
of items relating to the book go on increasing.
Defining a Structure
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;
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
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);}
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.
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);
}
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.
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);}
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