• facebook
  • whatsapp
  • telegram

Structures, Unions

User-defined variables: Structures, Unions and Enumerators
16.1 Introduction
C లాంగ్వేజీ లో ఉన్న variablesను ఈ కింది విధంగా విభజించవచ్చు.

C లాంగ్వేజీలో 2-D arrays ఉండటంతో matrix multiplication చేయగలిగాం. అంటే matrix multiplication problem tractable ( able to be done ) అయింది. ఇక్కడ, array అంటే ఒకేరకమైన elements లేదా members ఉండేది. అలాగే, కొన్ని ప్రాబ్లంలను చేయడానికి మనం user defined variables ను define చేస్తాం. వీటిలోsame members లేదా వేరే టైప్ members అయినా ఉండవచ్చు . C లాంగ్వేజీలో structures, unions , enumerators అనే user defined variables ఉన్నాయి. వీటి గురించి ఈ పాఠంలో నేర్ఛుకుందాం.

9.16.2 Structures
ఒక structure ను define చేయడానికి ముందుగా దానికి ఏ పేరు, ఏ మెంబర్లను ఇవ్వాలని అనుకుంటున్నారో నిర్ణయించుకోవాలి. దానిని ఈ కింది విధంగా define చేయాలి.
struct struct_typename
     {
          Type member1;
          Type member2;
          –
          Type membern;
};
ఉదాహరణకు, DATE అనే దానిని day (d), month (m), year (y) అనే మెంబర్లతో ఇలా define చేయవచ్చు. ఆఖరున } తర్వాత ; (సెమీకోలన్) పెట్టాలి.
struct DATE
{
     int d;
     int m;
     int y;
};

ఈ కింది declaration statement ఒక DATE టైపు variable Aను, ఒక DATE type pointer variable p ను declare చేస్తుంది.
struct DATE A, *p;

వీటిని structure definition తో పాటుగా ఈ కింది విధంగా కూడా declare చేయవచ్చు.
struct DATE
{
     int d;
     int m;
     int y
} A, *p;

ఇక్కడ, A కు, మూడు integer లు d, m, y లు మెంబర్లుగా ఉన్నాయి కాబట్టి, మూడు integers కు ఎంత కావాలో అంత memory allocate అవుతుంది. అంటే, 2+2+2=6 bytes అయినా లేదా 4+4+4=12 bytes అయినా allocate అవుతుంది. అలాగే, p అనేది pointer టైప్ కాబట్టి, దానికి 4 bytes allocate అవుతాయి.

9.16.2.1 Accessing data members

A అనేది DATE టైప్ structured variable, దానిలోఉండే మెంబర్లను .(dot) అనే delimiter (separator)ను వాడి A.d, A.m, A.y లుగా చెబుతాం. ఇవి integer లు. ఒక structured variable లోపలికి డేటా రీడ్ చేసేటప్పుడు లేదా ప్రింట్ చేసేటప్పుడు మెంబరు తర్వాత మెంబరును వాడతాం. ఈ కింది ప్రోగ్రాం దీనిని విశదీకరిస్తుంది. ఇది రెండు DATE టైప్ variables A,B ల్లోకి డాటా రీడ్ చేసి ప్రింట్ చేస్తుంది.
Example 1:
#include
struct DATE
{
     int d;
     int m;
     int y;
};
int main()
{
     struct DATE A,B;

     printf(“Memory Allocated for A=%d\n”, sizeof(A));
     printf(“Memory Allocated for B=%d\n”, sizeof(B));

     printf(“Enter two Dates\n”);
     scanf(“%d%d%d”, &A.d, &A.m, &A.y);
     scanf(“%d%d%d”, &B.d, &B.m, &B.y);

     printf(“Given Dates are\n”);
     printf(“%d\t%d\t%d\n”, A.d, A.m, A.y);
     printf(“%d\t%d\t%d\n”, B.d, B.m, B.y);
     return(0) ;
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

16.2.2 Accessing a structures data members through its pointer

ఒక structured variable మెంబర్లను దాని pointer ద్వారా కూడా access చెయ్యవచ్చు. p అనేది ఒక DATE టైప్ structured variable (A) కు pointer అయితే అందులో (Aలో) ఉండే మెంబర్లను de-referencing operator ( ->) ను p->d, p->m, p->y గా చెబుతాం. ఈ కింది ప్రోగ్రాం ద్వారా ఈ notationను ఎలా ఉపయోగించాలో తెలుసుకుందాం.

Example 2:
#include
struct DATE
{
     int d;
     int m;
     int y;
};
int main()
{
     struct DATE A, *P;
     printf(“Enter a Date\n”);
     scanf(“%d %d %d”, &A.d, &A.m, &A.y);

     P = &A; /* p is pointer to A*/
     /*All the following three ways of accessing members
     are acceptable*/

     printf(“%d\t%d\t%d\n”, A.d, A.m, A.y);
     printf(“%d\t%d\t%d\n”, P->d, P->m, P->y);
     printf(“%d\t%d\t%d\n”, (*P).d, (*P).m, (*P).y);
     /* Here, *p refers to A */
     return(0);
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

A కు 2004 memory allocate అయింది అనుకుంటే, p=&A అనేది రన్ అయిన తర్వాత, p కు 2004 ఇస్తుంది. ఇక్కడ, p అనేది A కు pointer, కానీ A లో ఉన్న ఏ మెంబరుకూ కాదు.
పై ప్రోగ్రాంలో, Aకు memory compiler allocate చేసినది. దానికి p pointer. ఈ కింది ప్రోగ్రాంలో, dynamic గా malloc ద్వారా create చేసిన ఒక DATE టైప్ variable addressను p అనే DATE టైప్ pointerకు ఇస్తే, ఆ dynamic గా create చేసిన DATE లోని మెంబర్లను ఎలా access చేయాలో చూపిస్తుంది. ఈ కింది ప్రోగ్రాంలో వాడిన notationను గుర్తుంచుకోవాలి.

Example 3:
#include
struct DATE
{
     int d;
     int m;
     int y;
};
int main()
{
     struct DATE *P;
     P = (struct DATE *)malloc(sizeof(struct DATE));
     printf(“Enter a Date\n”);
     scanf(“%d%d%d”, &P->d, &P->m, &P->y);
     /* The following elements can be accessed*/
     printf(“%d\t%d\t%d\n”, P->d, P->m, P->y);
     printf(“%d\t%d\t%d\n”, (*P).d, (*P).m, (*P).y);
     return(0) ;
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

9.16.2.3 Array of Structures

ఇక్కడ array of structures కూడా ఉపయోగించవచ్చు. ఉదాహరణకు, మూడు elements ఉండేటట్లు ఒక DATE టైప్ arrayను ఈ కింది విధంగా dedeclare చేయవచ్చు.

struct DATE A[3];

అప్పుడు మూడు DATE టైప్ variablesకు memory ఈ కింద చూపిన విధంగా ఇస్తాం.

ఇప్పుడు, A[0], A[1], మరియు A[2] అనేవి DATE type variables. అలాగే, A[0].d, A[0].m, A[0].y, అనేవి A[0] లోని మెంబర్లు. ఈ కింది ప్రోగ్రాం వీటిని ఉపయోగిస్తుంది. ఈ కింది ప్రోగ్రాంలో వాడిన notationను గుర్తుంచుకోవాలి.
Example 4:
#include
struct DATE
{
     int d;
     int m;
     int y;
};

int main()
{
     int i;
     struct DATE A[3]; /* A is DATE type of array */

     printf(“Enter three Dates\n”);

     for( i = 0; i < 3; i ++)
     scanf(“%d %d %d”, &A[i].d, &A[i].m, &A[i].y);
     printf(“Given Dates are\n”);
     for( i = 0; i < n; i ++)
     printf(“%d\t%d\t%d\n”, &A[i].d, &A[i].m, &A[i].y);
     return(0);
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

Example 5: ఈ ప్రోగ్రాంలో ఒక DATE టైపు structured array (A) ను dynamic గా malloc వాడి create చేస్తున్నాం. ఈ కింది ప్రోగ్రాంలో వాడిన notationను గుర్తుంచుకోవాలి. ఇక్కడ A అనేది array కు pointer. కాబట్టిA, A+1, A+2 లు A[0], A[1], A[2] లకు pointers. ఈ కింది పట్టికను చూడండి.

#include
struct DATE
{
     int d;
     int m;
     int y;
};
int main()
{
     int i;
     stuct DATE *A;
     A = (struct DATE *)malloc(3*sizeof(struct DATE));
     /* Creates a structured array dynamically */
     printf(“Enter Three Dates Data\n”);
     for( i = 0; i < 3; i ++)
     scanf(“%d%d%d”, &(A+[i]->.d, &(A+[i])->.m, &A+[i]->.y);

     printf(“Given Dates are\n”);
     for( i = 0; i < 3; i ++)
     {
     /* The following three notations are equivalent*/
     printf(“%d\t%d\t%d\n”, &A[i].d, &A[i].m, &A[i].y);
     printf(“%d\t%d\t%d\n”, (A + i)->d, (A + i)->m, (A + i)->y);
     printf(“%d\t%d\t%d\n”,(*(A+i)).d, (*(A+i)).m, (*(A+i)).y);
     }
     return(0);
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

16.2.4 Nested Structures
ఒక structure లోపల ఇంకో structure ను వాడవచ్చు.
struct STUD
{
     char name[20];
     int RNo;
     struct DATE
     {
          int d;
          int m;
          int y;
     }
     DOB;
};

పై ప్రోగ్రాంలో DATE definition STUD లోపల ఉంది కాబట్టి మనం DATE type ని బయట వాడలేం. కానీ పై దానినే ఈ విధంగా రాస్తే ఆ సమస్య ఉండదు.

struct DATE
     {
          int d;
          int m;
          int y;
     };
struct STUD
     {
          char name[20];
          int RNo;
          struct DATE DOB;
     };

Example 6: ఈ ప్రోగ్రాంలో STUD టైప్ variablesకు సంబంధించిన notations రాశాం.
#include
main()
{
     struct STUD A;
     printf(“Enter student name, roll no, and date of birth\n”);
     scanf(“%s%d%d%d%d”,&A.name, &A.RNo, &A.DOB.d, &A.DOB.m, &A.DOB.y);
     printf(“%s\t%d\t%d\n%d\t%d\n”, A.name, A.RNo, A.DOB.d, A.DOB.m, A.DOB.y);
     return(0);
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

Example 7: ఈ ప్రోగ్రాంలో STUD టైప్ variablesకు సంబంధించిన notations ఉన్నాయి. ఇందులో STUD టైప్ pointerను ఎలా వాడాలో, దానికి సంబంధించిన notations గురించి తెలుసుకుందాం.
#include
/*include the definition of above structures*/
int main()
{
     struct STUD A, *P;
     P = &A; /* P is pointer to A. Not to any member of it*/
     printf(“Enter student name, roll no, and date of birth\n”);

     scanf(“%s%d%d%d%d”,&A.name, &A.RNo, &A.DOB.d, &A.DOB.m, &A.DOB.y);
     /* The following three styles are equivalent*/

     printf(“%s\t%d\t%d\n%d\t%d\n”, A.name, A.RNo, A.DOB.d, A.DOB.m, A.DOB.y);
     printf(“%s\t%d\t%d\t%d\t%d\n”, P->name, P->RNo,P->DOB.d, P->DOB.m, P->DOB.y);
     printf(“%s\t%d\t%d\n%d\t%d\n”, (*P).name, (*P).RNo, (*P).DOB.d, (*P).DOB.m, (*P).DOB.y);
     return(0);
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

Example 8 : ఈ ప్రోగ్రాంలో STUD టైప్ pointer ఒక dynamic గా create అయిన ఒక STUDకు pointer అయితే, దానికి సంబంధించిన notations ఎలా ఉంటాయో చూద్దాం.

#include
main()
{
     structSTRUCT STUD *P;
     P = (struct STUD *)malloc(sizeof(struct STUD));
     printf(“Enter student name, roll no, and date of birth\n”);
     scanf(“%s%d%d%d%d”,&P->name,&P->RNo,&P->DOB.d, &P->DOB.m,&P->DOB.y);
     printf(“%s\t%d\t%d\t%d\t%d\n”, P->name, P->RNo, P->DOB.d, P->DOB.m, P->DOB.y);
     printf(“%s\t%d\t%d\t%d\t%d\n”, (*P).name, (*P).RNo, (*P).DOB.d, (*P).DOB.m, (*P).DOB.y);
     return 0;
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

16.2.5 Self Referencing Structures

పైన ఒక structure లో మరొక structure ను మెంబరుగా వాడాం. ఒక structure లోపల దాని లాంటి వాటినే members గా వాడవచ్చు. వీటిని Self Referencing Structures అని అంటారు. ఈ కింద lst అనే structure ను define చేస్తూ దానిలో next అనే lst టైప్ structured pointerను వాడాం. ఇలాంటి వాటిని data structures లో ఎక్కువగా ఉపయోగిస్తాం.

struct lst
{
     int n;
     struct lst *next;
};

Example 9: ఈ ప్రోగ్రాంలో ఒక simple linked list ఎలా create చేయాలో చూద్దాం.
#include
struct lst
{
     int n;
     struct lst *next;
};
int main()
{
     struct lst A,B,C, *H;

     A.n=100;
     /* A.n is an integer thus we are assigning 100.*/
     B.n=190;
     /* B.n is an integer thus we are assigning 190.*/
     C.n=11;
     /* C.n is an integer thus we are assigning 11.*/

     A.next=&B;

     /* We know A.next is a lst type of pointer and we are assigning address of a lst type of object (B). Thus, no violation of rules. */

     B.next=&C;

     /* We also know B.next is a lst type of pointer and we are assigning address of a lst type of object (C). Thus, no violation of rules. */

     C.next=0;
     /* We know A.next is a lst type of pointer and we are assigning 0. One can assign zero a a pointer. Thus, no violation of rules. */

     H=&A;
     /* We know H is a lst type of pointer and we are assigning address of a lst type of object (A). Thus, no violation of rules. */
     printf(Elements il the linked list are\n");

     while(H)
     {
     printf(“%d\n”, H->n);
     H=H->next;
     }
     return(0);
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

9.16.2.6 Initializing Data Members of a Structure
మనం structured variables మెంబర్లను initialize కూడా చేయవచ్చు. ఈ కింది ప్రోగ్రాం దానిని వివరిస్తుంది. ఉదాహరణకు, DATE type structured variableను ఇలా initialize చేయవచ్చు.

struct DATE A={10,12,2009}; అప్పుడు, A.d=10, A.m=12, A.y=2009 అవుతాయి.
Example 10:

#include
struct DATE
{
     int d;
     int m;
     int y;
};
int main()
{
     struct DATE A={10,12,2009},B[2]={{10,12,2008},{2,2,2009}},C[]= {{10,12,2008},{2,2,2009}};

     printf("%d\t%d\t%d\n", A.d, A.m, A.y);
     printf("%d\t%d\t%d\n", B[0].d, B[0].m, B[0].y);
     printf("%d\t%d\t%d\n", C[1].d, C[1].m, C[1].y);

     return(0) ;
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

Example 11: nested structured variables మెంబర్లను initialize చేసే ప్రోగ్రాం చూద్దాం.
#include
struct DATE
     {
     int d;
     int m;
     int y;
     };
struct STUD
     {

     char name[20];
     int RNo;
     struct DATE DOB;
     };
int main()
{
     struct STUD A={"Ram", 113,10,12,2009};
     printf("%s %d %d %d %d\n", A.name, A.RNo, A.DOB.d, A.DOB.m, A.DOB.y);
     return(0) ;
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

16.2.7 Passing structures to functions

మనం structured variablesను, structured pointer variablesను functions లోపలికి పంపించవచ్చు. ఈ కింది declaration లో A అనేది passing by value స్టైల్, p అనేది passing by address స్టైల్.
void xyz(struct DATE A, struct DATE *p);

Example 12: Function, PRINTDATE() అనేది ఒక DATE టైప్ variable ను argument గా తీసుకొని దాని మెంబర్ల విలువలను ప్రింట్ చేస్తుంది. READDATE() function ఒక DATE టైప్ variable address తీసుకుని దాని లోపలికి data రీడ్ చేస్తుంది.
#include
struct DATE
     {
     int d;
     int m;
     int y;
     };
void PRINTDATE(struct DATE A)
     {
          printf(“%d\t%d\t%d\n”, A.d, A.m, A.y);
     }
void READDATE(struct DATE *A)
      {
          scanf (“%d%d%d”, &A->d,&A-> m, &A->y);
     }
int main()
{
     struct DATE X;
     printf(“Enter Date value\n”);
     READDATE(&X);
     printf(“Data Given is\n”);
     PRINTDATE(X);
     return(0) ;
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

Example 13 : ఈ ప్రోగ్రాంలో PRINTDATE(), READDATE(), functions ను వాడి structured DATE array లోపలికి, dynamic structured DATE array లోపలికి డేటా ఎలా రీడ్ చేసి ఎలా ప్రింట్ చేయాలో చూపిస్తుంది.
#include
int main()
     {
     struct STRUCT DATE A[3], *X;,
     int i;
     printf(“Enter Three Dates \n”);
     for( i = 0; i < 3; i ++) READDATE(&A[i]);

     printf(“Given Dates are\n”);
     printf(“Given Dates are\n”);
     for( i = 0; i < 3; i ++) PRINTDATE(A[i]);

     X = (struct DATE *)malloc(3*sizeof(struct DATE));
     printf(“Enter Three Dates \n”);

     for( i = 0; i < 3; i ++) READDATE(X + i);
     printf(“Given Dates are\n”);

     for( i = 0; i < 3; i ++) PRINTDATE(X[i]);
     return(0);
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

9.16.2.8 Returning structures from functions

మనం structuresను, structured addresses ను functions నుంచి రిటర్న్ చేయవచ్చు. ఈ కింది ప్రోగ్రాం దీనిని explain చేస్తుంది. ఇది DATE టైప్ structured variable ని function నుంచి రిటర్న్ చేస్తుంది.
Example 14:

#include
struct DATE XYZ()
     {
     struct DATE A;
     printf(“Enter a Value for Date\n”);

     scanf("%d%d%d", &A.d, &A.m, &A.y);
     return A;
}
int main()
{
     struct DATE B;
     B=XYZ();
     printf(" Date read in the function = %d\t%d\t%d\n", B.d, B.m, B.y);
     return(0) ;
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

16.3 Unions
ఇది కూడా ఒక user defined type. చాలా తక్కువగా అవసరమవుతుంది. దీనిలో చాలా మెంబర్లు ఉన్నా, ఏదో ఒకటి మాత్రమే active లేదా meaningful గా ఉంటుంది.
ఉదాహరణకు ఒక warehouse manager, తన warehouse లో ఉన్న items వాటి LR number లేదా description తో నిర్వహిస్తాడు. అంటే ఏదో ఒకటే ఉంటుంది. అలాంటప్పుడు items ను define చేయడానికి Union వాడతాం.
union Item
{
     int LRNo;
     char desc[20];
};

Example 15: ఈ కింది ప్రోగ్రాంతో union Item ను ఎలా ఉపయోగించాలో తెలుసుకుందాం.
#include
union Item
{
     int LRNo;
     char desc[20];
};
int main()
{
     union Item A,B;
     printf("Enter LRNo and Description\n");
     scanf("%d%s", &A.LRNo, B.desc);
     printf("Correct way of accessing\n");
     printf("LRNo=%d Description %s\n", A.LRNo, B.desc);
     printf("In-Correct way of accessing\n");
     printf("LRNo=%s Description %d\n", A.desc, B.LRNo);

     return(0);
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.


9.16.4 Enumerations
వీటి ద్వారా symbolic constants ను define చేయవచ్చు. ఈ కింద ఇచ్ఛిన దాని ద్వారా VAL1, VAL2, అనే symbolic constants define అవుతాయి, వాటి విలువలు 0,1…లాగా ఉంటాయి.
enum enum_name
     {
          VAL1,
          VAL2,
          .....
          VAL9
     };

ఈ symbolic constants మనకు కావలసిన విలువలు ఇస్తూ కూడా enumeratorను define చేయవచ్ఛు.
enum enum_name
{
     VAL1=1,
     VAL2=70,
     VAL3,
     VAL4=90,
     .....
     VAL9
};

ఇప్పుడు ఈ కింది దాని ద్వారా TRUE, FALSE symbolic constants ను define చేస్తున్నాం.
enum
{
     FALSE,
     TRUE
};

ఈ కింది ప్రోగ్రాం segmentలో వీటిని వాడుతున్నాం.
switch(answer)
{
     case FALSE: ----
          ---
          break;
     case TRUE: __
          ----
          break;
}
ఈ కింది దానితో పొలిస్తే ఇది కొద్దిగా సులభంగా అర్ధమవుతుంది నిజానికి రెండూ ఒకే పని చేస్తాయి.
switch(answer)
{
     case 0: ----
     ---
     break;
     case 1: __
     ----
     break;
}

16.5 Typedef
ప్రోగ్రాంల రీడబిలిటిని పెంచేందుకు, typedef ద్వారా కొత్త పేర్లు int, float, double టైప్ వాటికి బదులుగా వాడవచ్చు. ఉదాహరణకి, ఈ కింద int కు వేరే పేరు LENGTH అని ఇస్తుంది.
typedef int LENGTH;
ఇప్పుడు కింద ఇచ్చిన రెండు statements లోనూ length, width, height అనేవి int టైపే. కానీ మొదటి statement కొద్దిగా చదవడానికి బాగుంటుంది.
LENGTH length, width, height;
int length, width, height;

అదే విధంగా మనం define చేసిన STUD structureకు, ఈ కింది విధంగా వేరే పేరు ఇవ్వవచ్చు.
typedef struct STUD PEOPLE;

ఇప్పుడు ఈ కింద ఇచ్చిన రెండూ ఒకటే.
struct STUD A;
PEOPLE A;


 

 

 

NB Venkateswarlu
M.Tech(IIT-Kanpur)
Ph.D (BITS),PDF(UK),
Sr.Prof., CSE, AITAM, Tekkali

Posted Date : 04-02-2021 .