0% found this document useful (0 votes)
5 views15 pages

Understanding C Structures and Usage

The document explains the concept of structures in C programming, which are user-defined data types that group variables of different types under a single name. It provides examples of how to declare, initialize, and access structures, as well as how to use arrays of structures and pointers to structures. The document also illustrates the advantages of using structures to manage related data efficiently.

Uploaded by

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

Understanding C Structures and Usage

The document explains the concept of structures in C programming, which are user-defined data types that group variables of different types under a single name. It provides examples of how to declare, initialize, and access structures, as well as how to use arrays of structures and pointers to structures. The document also illustrates the advantages of using structures to manage related data efficiently.

Uploaded by

patelsharad275
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structure

Structure is a user defined datatype that groups variable of different data


types under a single name.

For storing information of two student-


(i) Student 1-
int rollnos1;
char names1[20];
float markss1;

(ii) Student 2-
int rollnos2;
char names2[20];
float markss2;

We have to take 6 variables.


And suppose if we want to store information of 50 students then we have
to take 150 variables.

To solve this problem, C provides a special data type—the structure.


A structure contains a number of data types grouped together.
These data types may or may not be of the same type.
struct student
{
int rollno;
char name[20];
float marks;
}s1,s2;

Example 2- 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, another for
storing prices and still another for storing number of pages.
(b) Use a structure variable.

void main( )
{
char name[3] ;
float price[3] ;
int pages[3], i ;
printf ( "\nEnter names, prices and no. of pages of 3 books\n" ) ;
for ( i = 0 ; i <= 2 ; i++ )
scanf ( "%c %f %d", &name[i], &price[i], &pages[i] );
printf ( "\nAnd this is what you entered\n" ) ;
for ( i = 0 ; i <= 2 ; i++ )
printf ( "%c %f %d\n", name[i], price[i], pages[i] );
}
The program becomes more difficult to handle as the number of
items relating to the book go on increasing. For example, we
would be required to use a number of arrays, if we also decide to
store name of the publisher, date of purchase of book, etc. To solve this
problem, C provides a special data type—the structure.
void main( )
{
struct book
{
char name ;
float price ;
int pages ;
};
struct book b1, b2, b3 ;
printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
scanf ( "%c %f %d", &[Link], &[Link], &[Link] ) ;
scanf ( "%c %f %d", &[Link], &[Link], &[Link] ) ;
scanf ( "%c %f %d", &[Link], &[Link], &[Link] ) ;
printf ( "\nAnd this is what you entered" ) ;
printf ( "\n%c %f %d", [Link], [Link], [Link] ) ;
printf ( "\n%c %f %d", [Link], [Link], [Link] ) ;
printf ( "\n%c %f %d", [Link], [Link], [Link] ) ;
}

Declaring a Structure-
struct student
{
char name[20] ;
int rollno ;
float marks ;
};
This statement defines a new data type called struct student.
The general form of a structure declaration statement is
given below:
struct <structure name>
{
structure element 1 ;
structure element 2 ;
structure element 3 ;
......
......
};
Once the new structure data type has been defined one or more
variables can be declared to be of that type.

How to declare variable for structure-


struct student
{
char name[20] ;
int rollno ;
float marks ;
};
1. struct student s1,s2;
2. struct student
{
char name[20] ;
int rollno ;
float marks ;
} s1,s2;

Initialization-
1. struct student
{
char name[20] ;
int rollno ;
float marks ;
};
void main
{
struct student s1={"Abcd", 1, 90.91};
struct student s2={"Efgh", 2, 95.91};
}

2. struct student
{
char name[20] ;
int rollno ;
float marks ;
} s1={"Abcd", 1, 90.91};

Program-
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20] ;
int rollno ;
float marks ;
};
void main()
{
struct student s1={"Abcd", 1, 90.91};
struct student s2={"Efgh", 2, 95.91};
printf("Info of student1");
printf("\n%s%d%f",[Link],[Link],[Link]);
printf("Info of student2");
printf("\n%s%d%f",[Link],[Link],[Link]);
}
Accessing Structure Elements-
In arrays we can access individual elements of an array using a
subscript. Structures use a different scheme. They use a dot (.)
operator.
[Link]
[Link]
[Link]
printf("%d", [Link]);

Runtime initialization-
struct student
{
char name[20] ;
int rollno ;
float marks ;
} s1;
void main()
{
printf("Enter info of first student");
scanf("%s",&[Link]);
scanf("%d",&[Link]);
scanf("%f",&[Link]);
}

Program-
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20] ;
int rollno ;
float marks ;
};
void main()
{
struct student st;
printf("Enter student name\n");
gets(&[Link]);
printf("Enter student roll no\n");
scanf("%d",&[Link]);
printf("Enter student marks\n");
scanf("%f",&[Link]);
printf("Student name: %s", [Link]);
printf("Student rollno: %d", [Link]);
printf("Student marks: %f", [Link]);
}

Array of Structures-
To store data of 60 students we would be required to use 60 different
structure variables from s1 to s60, which is definitely not very
convenient. A better approach would be to use an array of structures.
void main( )
{
struct student
{
char name[20] ;
int rollno ;
float marks ;
};
struct student st[60] ;
int i ;
for ( i = 0 ; i <60 ; i++ )
{
printf ( "\nEnter student name, roll no and marks" ) ;
scanf ( "%s %d %f", &st[i].name, &st[i].rollno, &st[i].marks) ;
}
for ( i = 0 ; i < 60 ; i++ )
printf ( "\n%s %d %f", st[i].name, st[i].rollno, st[i].marks) ;
}
Passing Structure as a Function Argument-
A structure can be passed as a function argument just like any other
variable.
void disp(struct student);
struct student
{
char name[20];
int rollno;
};
void main()
{
struct student st;
printf("Enter student name");
gets(&[Link]);
printf("Enter student roll no");
scanf("%d", &[Link]);
disp(st);
}
void disp(struct student stu)
{
printf("Student name: %s", [Link]);
printf("Student roll no: %d", [Link]);
}
Pointer to structure-
A structure pointer is defined as the pointer which points to the address of the
memory block that stores a structure known as the structure pointer. The structure
pointer tells the address of a structure in memory by pointing the variable to the
structure variable.
The structure pointer points to the address of a memory block where the
Structure is being stored. Like a pointer that tells the address of another
variable of any data type (int, char, float) in memory. We use a structure
pointer which tells the address of a structure in memory by pointing pointer
variable ptr to the structure variable. Complex data structures like Linked
lists, trees, graphs, etc. are created with the help of structure pointers.

Declaration Structure Pointer


The declaration of a structure pointer is similar to the declaration of the structure
variable. So, we can declare the structure pointer and variable inside and outside of
the main() function.
struct structure_name *ptr;

Initialization of the Structure Pointer


ptr = &structure_variable;

We can also initialize a Structure Pointer directly during the declaration of a


pointer.
struct structure_name *ptr = &structure_variable;

Accessing members of structure using structure pointer.


Accessing the Elements of a Structure
To access the elements of a structure with pointer, we use a special operator called
the indirection operator (→) .
We define a user-defined struct type called book. We declare a book variable and a
pointer.
struct book{
char title[10];
double price;
int pages;
};
struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;//strptr is a pointer to some variable of type struct book
To store the address, use the & operator.
strptr = &b1;

Using the Indirection Operator-


In C programming, we use the indirection operator ("→") with struct pointers. It is
also called the "struct dereference operator". It helps to access the elements of a
struct variable to which the pointer references to.

To access an individual element in a struct, the indirection operator is used as


follows −
strptr -> title;
strptr -> price;
strptr -> pages;
The struct pointer uses the indirection operator or the dereference operator to fetch
the values of the struct elements of a struct variable. The dot operator (".") is used
to fetch the values with reference to the struct variable. Hence,
[Link] is the same as strpr -> title
[Link] is the same as strptr -> price
[Link] is the same as strptr -> pages

struct book{
char title[10];
double price;
int pages;
};
int main(){
struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;
strptr = &b1;

printf("Title: %s\n", strptr -> title);//(*strptr).title


printf("Price: %lf\n", strptr -> price);
printf("No of Pages: %d\n", strptr -> pages);

return 0;
}

Output
Title: Learn C
Price: 675.500000
No of Pages: 325
The dot operator (.) is used to access the struct elements via the struct variable.
To access the elements via its pointer, we must use the indirection operator (→).

You might also like