C lecture 6.2
C lecture 6.2
Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their
member.
In other words, structures pointing to the same type of structures are self-referential in nature.
struct node {
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob;
return 0;
}
In the example ‘link’ is a pointer to a structure of type ‘node’. Hence, the structure ‘node’ is a self-referential structure with
‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before accessing, as by default it contains
garbage value.
Types of Self Referential Structures:
Self Referential Structure with Single Link
Self Referential Structure with Multiple Links
Self Referential Structure with Single Link: These structures can have only one self-pointer as their member. The following
example will show us how to connect the objects of a self-referential structure with the single link and access the
corresponding data members.
Self Referential Structure with Multiple Links:
Self referential structures with multiple links can have more than one self-pointers. Many complicated data structures can be
easily constructed using these structures. Such structures can easily connect to more than one nodes at a time. The
following example shows one such structure with more than one links.
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
void printBook( struct Books *book );
int main( ) {
struct Books Book1;
struct Books Book2;
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "CP");
Book1.book_id = 6495407;
strcpy( Book2.title, "Computer Organization and Architecture");
strcpy( Book2.author, "Kai Wang");
strcpy( Book2.subject, "COA");
Book2.book_id = 6495700;
printBook( &Book1 );
printBook( &Book2 );
return 0;
}
void printBook( struct Books *book ) {
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}
C Unions:
A union is a user-defined type similar to structs in C except for one key difference. Structs allocate enough space to
store all its members wheres unions allocate the space to store only the largest member.
How to define a union?
We use the union keyword to define unions.
union car
{
char name[50];
int price;
};
The above code defines a derived type union car.
Create union variables:
When a union is defined, it creates a user-defined type. However, no memory is allocated. To allocate memory for a given union type and work with it, we need to create variables.
union car
{
char name[50];
int price;
};
int main()
{
union car car1, car2, *car3;
return 0;
}
Another way of creating union variables is:
union car
{
char name[50];
int price;
} car1, car2, *car3;
Access members of a union:
We use the . operator to access members of a union.
To access pointer variables, we use also use the -> operator.
#include <stdio.h>
#include <string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
strcpy(record1.name, "Raju");
strcpy(record1.subject, "Maths");
record1.percentage = 86.50;
printf("Union record1 values example\n");
printf(" Name : %s \n", record1.name);
printf(" Subject : %s \n", record1.subject);
printf(" Percentage : %f \n\n", record1.percentage);
printf("Union record2 values example\n");
strcpy(record2.name, "Mani");
printf(" Name : %s \n", record2.name);
strcpy(record2.subject, "Physics");
printf(" Subject : %s \n", record2.subject);
record2.percentage = 99.50;
printf(" Percentage : %f \n", record2.percentage);
return 0;
}
Array of Unions:
Definition:
union student
{
int rollno;
char name[20];
char div;
};
Declaration of array of union:
union union_name union_variable_name[size_of_array];
Declaration of array of union student:
void main()
{
union student s[10];
}
Nested Union:
#include <stdio.h>
#include <malloc.h>
int main()
{
union A
{
long int y[5];
union B
{
double g;
union C
{
int k;
union D
{
char ch;
int x[5];
}s;
}a;
}b;
}*p;
p = (union A*) malloc(sizeof(union A));
p->b.a.k = 15;
printf("%d ,%d", p->b.a.s.x[0], p->y[0] );
}
Differences between Structures and Unions:
Enumerated data:
Enumeration is a user defined datatype in C language. It is used to assign names to the integral constants which makes
a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.
Here is the syntax of enum in C language,
The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of
enum type as follows.
There are multiple advantages of using enum over macro when many related named constants have integral values.
a) Enums follow scope rules.
b) Enum variables are automatically assigned values. Following is simpler
enum state {Working, Failed, Freezed};