0% found this document useful (0 votes)
36 views

Structures

The document explains the concept of structures in the C programming language, which are composite data types that group different variables under a single name. It details how to declare and access structure members, the differences between variables, arrays, and structures, and provides examples of using structures in functions and with pointers. Additionally, it covers nested structures and bit fields, illustrating their usage with code snippets.

Uploaded by

clashsai056
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Structures

The document explains the concept of structures in the C programming language, which are composite data types that group different variables under a single name. It details how to declare and access structure members, the differences between variables, arrays, and structures, and provides examples of using structures in functions and with pointers. Additionally, it covers nested structures and bit fields, illustrating their usage with code snippets.

Uploaded by

clashsai056
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Structures

A struct in the C programming language (and many derivatives) is


a composite data type declaration that defines a physically grouped list of
variables to be placed under one name in a block of memory, allowing the
different variables to be accessed via a single pointer, or the struct
declared name which returns the same address. The struct can contain
many other complex and simple data types in an association, so is a
natural organizing type for records like the mixed data types in lists of
directory entries reading a hard drive (file length, name, extension, physical
(cylinder, disk, head indexes) address, etc.), or other mixed record type
(patient names, address, telephone... insurance codes, balance, etc.).

Arrays allow to define type of variables that can hold several data items of
the same kind. Similarly structure is another user defined data type
available in C that allows to combine data items of different kinds. The
format of the struct statement is as follows:

struct [structure tag] {


member definition;
member definition;
...
member definition;
} [one or more structure variables];
The structure tag is optional and each member definition is a normal
variable definition, such as int i; or float f; or any other valid variable
definition. At the end of the structure's definition, before the final semicolon,
you can specify one or more structure variables but it is optional. Here is
the way you would declare the Book structure
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
C – Structure
C Structure is a collection of different data types which are
grouped together and each element in a C structure is called
member.

 If you want to access structure members in C, structure


variable should be declared.
 Many structure variables can be declared for same structure
and memory will be allocated for each separately.
 It is a best practice to initialize a structure to null while
declaring, if we don’t assign any values to structure
members.
DIFFERENCE BETWEEN C VARIABLE, C ARRAY AND C
STRUCTURE:
 A normal C variable can hold only one data of one data type
at a time.
 An array can hold group of data of same data type.
 A structure can hold group of data of different data types
and Data types can be int, char, float, double and long
double etc.
C Structure:

struct student
{
int a;
char b[10];
Syntax }
a = 10;
Example b = “Hello”;
C Variable:

Syntax: int a;
int Example: a = 20;
Syntax: char b;
char Example: b=’Z’;
C Array:
Syntax: int a[3];
Example:
a[0] = 10;
a[1] = 20;
a[2] = 30;
int a[3] = ‘\0’;
Syntax: char b[10];
Example:
char b=”Hello”;

Using normal Using pointer


variable variable
Syntax: Syntax:
struct tag_name struct tag_name
{ {
data type data type
var_name1; var_name1;
data type data type
var_name2; var_name2;
data type data type
var_name3; var_name3;
}; };
Example: Example:
struct student struct student
{ {
int mark; int mark;
char name[10]; char name[10];
float average; float average;
}; };
Declaring Declaring
structure using structure using
normal variable: pointer variable:
struct student struct student
report; *report, rep;
Initializing Initializing
structure using structure using
normal variable: pointer variable:
struct student struct student rep
report = {100, = {100, “Mani”,
“Mani”, 99.5}; 99.5};
report = &rep;

Accessing Accessing
structure membe structure membe
rs using normal rs using pointer
variable: variable:
report.mark; report -> mark;
report.name; report -> name;
report.average; report -> average;

1 #include <stdio.h>
2 #include <string.h>
3
4 struct student
5
{
6
7 int id;
8 char name[20];
9 float percentage;
1 };
0
1 int main()
1 {
1 struct student record = {0};
2
1 //Initializing to null
3
1 record.id=1;
4 strcpy(record.name, "Raju");
1 record.percentage = 86.5;
5
1 printf(" Id is: %d \n", record.id);
6
1
7
1
8 printf(" Name is: %s \n",
1 record.name);
9
printf(" Percentage is: %f \n",
2
0 record.percentage);
2 return 0;
1}
2
2
2
3

Accessing Structure Members


To access any member of a structure, we use the member access operator (.).
The member access operator is coded as a period between the structure variable
name and the structure member that we wish to access. You would use the
keyword struct to define variables of structure type. The following example shows
how to use a structure in a program

#include <stdio.h>
#include <string.h>

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};

int main( ) {

struct Books Book1; /* Declare Book1 of type Book */


struct Books Book2; /* Declare Book2 of type Book */

/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;

/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;

/* print Book1 info */


printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */


printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);

return 0;
}

When the above code is compiled and executed, it produces the following result −
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};

int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};
struct student record2 = {2, "Mani", 93.5};
printf("Records of STUDENT1: \n");
printf(" Id is: %d \n", record1.id);
printf(" Name is: %s \n", record1.name);
printf(" Percentage is: %f \n\n", record1.percentage);
printf("Records of STUDENT2: \n");
printf(" Id is: %d \n", record2.id);
printf(" Name is: %s \n", record2.name);
printf(" Percentage is: %f \n\n", record2.percentage);
return 0;
}
OUTPUT:
Records of STUDENT1:
Id is: 1
Name is: Raju
Percentage is: 90.500000
Records of STUDENT2:
Id is: 2
Name is: Mani
Percentage is: 93.500000

C – Array of Structures
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};

int main()
{
int i;
struct student record[60];

// 1st student's record


record[0].id=1;
strcpy(record[0].name, "Raju");
record[0].percentage = 86.5;

// 2nd student's record


record[1].id=2;
strcpy(record[1].name, "Surendren");
record[1].percentage = 90.5;

// 3rd student's record


record[2].id=3;
strcpy(record[2].name, "Thiyagu");
record[2].percentage = 81.5;

for(i=0; i<3; i++)


{
printf(" Records of STUDENT : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %f\n\n",record[i].percentage);
}
return 0;
}
Records of STUDENT : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000

Records of STUDENT : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000

Records of STUDENT : 3
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000

Structures as Function Arguments


You can pass a structure as a function argument in the same way as you pass any
other variable or pointer.

#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printBook( Book1 );
/* Print Book2 info */
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);
}
When the above code is compiled and executed, it produces the following result −
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

Nested Structures
1. #include <stdio.h>
2. #include <string.h>
3. struct Employee
4. {
5. int id;
6. char name[20];
7. struct Date
8. {
9. int dd;
10. int mm;
11. int yyyy;
12. }doj;
13. }e1;
14. int main( )
15. {
16. //storing employee information
17. e1.id=101;
18. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
19. e1.doj.dd=10;
20. e1.doj.mm=11;
21. e1.doj.yyyy=2014;
22.
23. //printing first employee information
24. printf( "employee id : %d\n", e1.id);
25. printf( "employee name : %s\n", e1.name);
26. printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1
.doj.yyyy);
27. return 0;
28. }

Output:

employee id : 101
employee name : Sonoo Jaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014

Pointers to Structures
You can define pointers to structures in the same way as you define pointer to any
other variable −

struct Books *struct_pointer;

Now, you can store the address of a structure variable in the above defined pointer
variable. To find the address of a structure variable, place the '&'; operator before
the structure's name as follows −

struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must
use the → operator as follows −

struct_pointer->title;

Let us re-write the above example using structure pointer.

#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
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);
}

When the above code is compiled and executed, it produces the following result −
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

Bit Fields
Bit Field Declaration
The declaration of a bit-field has the following form inside a structure −

struct {

type [member_name] : width ;

};

The following table describes the variable elements of a bit field −

Sr.No Element & Description


.

1
type

An integer type that determines how a bit-field's value is interpreted.


The type may be int, signed int, or unsigned int.

2
member_name

The name of the bit-field.

3
width

The number of bits in the bit-field. The width must be less than or equal
to the bit width of the specified type.

The variables defined with a predefined width are called bit fields. A bit
field can hold more than a single bit; for example, if you need a variable
to store a value from 0 to 7, then you can define a bit field with a width of
3 bits as follows −

struct {

unsigned int age : 3;


} Age;

Suppose your C program contains a number of TRUE/FALSE variables


grouped in a structure called status, as follows −

struct {

unsigned int widthValidated;

unsigned int heightValidated;

} status;

This structure requires 8 bytes of memory space but in actual, we are


going to store either 0 or 1 in each of the variables. The C programming
language offers a better way to utilize the memory space in such
situations.

If you are using such variables inside a structure then you can define the
width of a variable which tells the C compiler that you are going to use
only those number of bytes. For example, the above structure can be re-
written as follows −

struct {

unsigned int widthValidated : 1;

unsigned int heightValidated : 1;

} status;

The above structure requires 4 bytes of memory space for status


variable, but only 2 bits will be used to store the values.

If you will use up to 32 variables each one with a width of 1 bit, then also
the status structure will use 4 bytes. However as soon as you have 33
variables, it will allocate the next slot of the memory and it will start
using 8 bytes. Let us check the following example to understand the
concept −

#include <stdio.h>
#include <string.h>

/* define simple structure */

struct {

unsigned int widthValidated;

unsigned int heightValidated;

} status1;

/* define a structure with bit fields */

struct {

unsigned int widthValidated : 1;

unsigned int heightValidated : 1;

} status2;

int main( ) {

printf( "Memory size occupied by status1 : %d\n", sizeof(status1));

printf( "Memory size occupied by status2 : %d\n", sizeof(status2));

return 0;

When the above code is compiled and executed, it produces the following
result −
Memory size occupied by status1 : 8
Memory size occupied by status2 : 4

// A simple representation of date


struct date
{
unsigned int d;
unsigned int m;
unsigned int y;
};

int main()
{
printf("Size of date is %d bytes\n", sizeof(struct date));
struct date dt = {31, 12, 2014};
printf("Date is %d/%d/%d", dt.d, dt.m, dt.y);
}
Output:

Size of date is 12 bytes


Date is 31/12/2014

The above representation of ‘date’ takes 12 bytes on a compiler where an unsigned


int takes 4 bytes. Since we know that the value of d is always from 1 to 31, value of
m is from 1 to 12, we can optimize the space using bit fields.
#include <stdio.h>

// A space optimized representation of date


struct date
{
// d has value between 1 and 31, so 5 bits
// are sufficient
unsigned int d: 5;

// m has value between 1 and 12, so 4 bits


// are sufficient
unsigned int m: 4;

unsigned int y;
};

int main()
{
printf("Size of date is %d bytes\n", sizeof(struct date));
struct date dt = {31, 12, 2014};
printf("Date is %d/%d/%d", dt.d, dt.m, dt.y);
return 0;
}
Output:

Size of date is 8 bytes

Date is 31/12/2014

struct {

type [member_name] : width ;

};

#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};

int main()
{
struct person *ptr;
int i, num;
printf("Enter number of persons: ");
scanf("%d", &num);

for(i = 0; i < num; ++i)


{
printf("Enter name, age and weight of the person
respectively:\n");
scanf("%s%d%f", &(ptr+i)->name, &(ptr+i)->age,
&(ptr+i)->weight);
}

printf("Displaying Infromation:\n");
for(i = 0; i < num; ++i)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->age,
(ptr+i)->weight);

return 0;
}

Output
Enter number of persons: 2
Enter name, age and weight of the person respectively:
Adam
2
3.2
Enter name, age and weight of the person respectively:
Eve
6
2.3
Displaying Information:
Adam 2 3.20 Eve 6 2.30

Unions
A union is a special data type available in C that allows to store different data
types in the same memory location. You can define a union with many members,
but only one member can contain a value at any given time. Unions provide an
efficient way of using the same memory location for multiple-purpose.

Defining a Union
To define a union, you must use the union statement in the same way as
you did while defining a structure. The union statement defines a new
data type with more than one member for your program. The format of
the union statement is as follows −

union [union tag] {


member definition;
member definition;
...
member definition;
} [one or more union variables];

The union tag is optional and each member definition is a normal


variable definition, such as int i; or float f; or any other valid variable
definition. At the end of the union's definition, before the final semicolon,
you can specify one or more union variables but it is optional. Here is the
way you would define a union type named Data having three members i,
f, and str −
union Data {
int i;
float f;
char str[20];
} data;

Now, a variable of Data type can store an integer, a floating-point


number, or a string of characters. It means a single variable, i.e., same
memory location, can be used to store multiple types of data. You can
use any built-in or user defined data types inside a union based on your
requirement.
The memory occupied by a union will be large enough to hold the largest
member of the union. For example, in the above example, Data type will
occupy 20 bytes of memory space because this is the maximum space
which can be occupied by a character string. The following example
displays the total memory size occupied by the above union −

#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %d\n",
sizeof(data));
return 0;
}

When the above code is compiled and executed, it produces the following
result −
Memory size occupied by data : 20

Accessing Union Members


To access any member of a union, we use the member access
operator (.). The member access operator is coded as a period between
the union variable name and the union member that we wish to access.
You would use the keyword union to define variables of union type. The
following example shows how to use unions in a program −

#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following
result −
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming
Here, we can see that the values of i and f members of union got
corrupted because the final value assigned to the variable has occupied
the memory location and this is the reason that the value of str member
is getting printed very well.

Now let's look into the same example once again where we will use one
variable at a time which is the main purpose of having unions −

#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following result −

data.i : 10
data.f : 220.500000
data.str : C Programming

Here, all the members are getting printed very well because one member is being used at a time.

Enumeration (or enum) in C


Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants,
the names make a program easy to read and maintain.
enum State {Working = 1, Failed = 0};
The keyword ‘enum’ is used to declare new enumeration types in C and C++. Following is an example of enum
declaration.
// The name of enumeration is "flag" and the constant
// are the values of the flag. By default, the values
// of the constants are as follows:
// constant1 = 0, constant2 = 1, constant3 = 2 and
// so on.
enum flag{constant1, constant2, constant3, ....... };
Variables of type enum can also be defined. They can be defined in two ways:
// In both of the below cases, "day" is
// defined as the variable of type week.

enum week{Mon, Tue, Wed};


enum day;

// Or

enum week{Mon, Tue, Wed}day;

// An example program to demonstrate working


// of enum in C
#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}

Output:
2
In the above example, we declared “day” as the variable and the value of “Wed” is allocated to day, which is 2.
So as a result, 2 is printed.
Another example of enumeration is:

// Another example program to demonstrate working


// of enum in C
#include<stdio.h>

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul,


Aug, Sep, Oct, Nov, Dec};

int main()
{
int i;
for (i=Jan; i<=Dec; i++)
printf("%d ", i);

return 0;
}

Output:
0 1 2 3 4 5 6 7 8 9 10 11
In this example, the for loop will run from i = 0 to i = 11, as initially the value of i is Jan which is 0 and the
value of Dec is 11.

Interesting facts about initialization of enum.


1. Two enum names can have same value. For example, in the following C program both ‘Failed’ and ‘Freezed’
have same value 0.
#include <stdio.h>
enum State {Working = 1, Failed = 0, Freezed = 0};

int main()
{
printf("%d, %d, %d", Working, Failed, Freezed);
return 0;
}

Output:
1, 0, 0

2. If we do not explicitly assign values to enum names, the compiler by default assigns values starting from 0.
For example, in the following C program, sunday gets value 0, monday gets 1, and so on.

#include <stdio.h>
enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday};

int main()
{
enum day d = thursday;
printf("The day number stored in d is %d", d);
return 0;
}

Output:
The day number stored in d is 4

3. We can assign values to some name in any order. All unassigned names get value as value of previous name
plus one.

#include <stdio.h>
enum day {sunday = 1, monday, tuesday = 5,
wednesday, thursday = 10, friday, saturday};

int main()
{
printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,
wednesday, thursday, friday, saturday);
return 0;
}

Output:
1 2 5 6 10 11 12

4. The value assigned to enum names must be some integeral constant, i.e., the value must be in
range from minimum possible integer value to maximum possible integer value.

5. All enum constants must be unique in their scope. For example, the following program fails in
compilation.

enum state {working, failed};


enum result {failed, passed};

int main() { return 0; }

Output:
Compile Error: 'failed' has a previous declaration as 'state failed'

Exercise:
Predict the output of following C programs
Program 1:

#include <stdio.h>
enum day {sunday = 1, tuesday, wednesday, thursday, friday, saturday};

int main()
{
enum day d = thursday;
printf("The day number stored in d is %d", d);
return 0;
}

Program 2:

#include <stdio.h>
enum State {WORKING = 0, FAILED, FREEZED};
enum State currState = 2;

enum State FindState() {


return currState;
}

int main() {
(FindState() == WORKING)? printf("WORKING"): printf("NOT WORKING");
return 0;
}

typedef in C
typedef keyword is used to assign a new name to a type. This is used just to prevent us from writing
more.
For example, if we want to declare some variables of type unsigned int, we have to write unsigned
int in a program and it can be quite hectic for some of us. So, we can assign a new name of our
choice for unsigned int using typedef which can be used anytime we want to use unsigned int in a
program.

typedef current_name new_name;

typedef unsigned int uint;


uint i, j;

Now, we can write uint in the whole program instead of unsigned int. The above code is the same as
writing:
unsigned int i, j;
Let's see an example.
#include <stdio.h>
int main() {
typedef unsigned int ui;
ui i = 5, j = 8;
printf("i = %d\n", i);
printf("j = %d\n", j);
return 0;
}
Output
i=5
j=8

What is a Self-Referential Structure?


A structure that contains pointers to a structure of its own type is known
as self-referential structure.

In other words, a self-referential C structure is the one which includes a


pointer to an instance of itself.

Self Referential Structure in C programming with Insertion, Deletion,


Traversal, Reverse, Adding and other opertions of Linked Lists
Syntax of Self-Referential Structure in C Programming
1struct demo
{
datatype member1, member2;
struct demo *ptr1, *ptr2;
}
As you can see in the syntax, ptr1 and ptr2 are structure pointers that
are pointing to the structure demo, so structure demo is a self referential
structure. These types of data structures are helpful in implementing
data structures like linked lists and trees. It is an error to use a structure
variable as a member of its own struct type structure or union type union,
respectively.

Self Referential Structure Example


struct node
{
int data;
struct node *next,*prev;
};
The concept of linked lists, stacks, queues, trees and many others works
on the principle of self-referential structures.
#include <stdio.h>
void main()
{
int a=10,b=20;
print(a);
print(b);
}
void print(int p)
{
printf("value is %d\n",p);
}
#include <stdio.h>
#include <string.h>
struct students {
int hallticket;
char name[100];
long int mobileno;
char department[100];
};
void studentdetails( struct students student );
int main( ) {
struct students student1;
struct students student2;
struct students student3;
student1.hallticket=123456;
strcpy( student1.name, "Divya Mehra");
student1.mobileno=9156327898;
strcpy(student1.department , "Mechanical");
student2.hallticket=1231234;
strcpy( student2.name, "Ramakrishna");
student2.mobileno=9848123456;
strcpy(student2.department , "Mechanical");
student3.hallticket=123685;
strcpy( student3.name, "Vishwanadh");
student3.mobileno=9900123456;
strcpy(student3.department , "Mechanical");
studentdetails(student1);
studentdetails(student2);
studentdetails(student3);
}
void studentdetails( struct students student )
{
printf("Halticket Number:%d\n",student.hallticket);
printf("Name:%s\n",student.name);
printf("Mobile Number:%ld\n",student.mobileno);
printf("Department:%s\n",student.department);
}

You might also like