Unit - 8
Unit - 8
Jigar Dalvadi
UNIT-8 - Structure
Basics of Pointer
Structure in C programming is very helpful in cases where we need to store similar data
of multiple entities.
Structure in C is a user-defined data types.
It is used to bind two or more similar or different data types or data structures together
into a single type. Like for storing the details of a student
Structure is created using the struct keyword, and a structure variable is created using
the struct keyword.
Syntax:
struct structure_name
{
Data_member_type data_member_defination;
Data_member_type data_member_defination;
Data_member_type data_member_defination;
...
...
} (structure_variables);
Example:
struct Student
{
char name[50];
int class;
int roll_no;
} student1;
Fdghf
fgngf
#include <stdio.h>
#include <string.h>
1|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi
int main() {
return 0;
}
Array of Structure
#include<stdio.h>
struct Employee
{
char ename[10];
int sal;
};
{
printf("\nEmployee name is %s", emp[i].ename);
printf("\nSlary is %d", emp[i].sal);
}
}
void main()
{
ask();
}
STRUCTURE FUNCTION
user defined datatypes derived data types
structures is not a function but only a datatype function returns a value
which returns no value
In Structure we can make one data member as Function does not have a provision of self
a pointer to the same structure. That is self referencial pointers.
referential structures.
Structures is a special datatype which holds all Function s consists of formal as well as actual
the possible form of data (i.e) int, arguments.
char,float,double ,in combinations
Structures we can do as above,we must access Functions are reusable anytime and anywhere
the data using '.' or when comes to pointers we in the program, whenever we wann to re-use
must access through '->' operator the code again we can simply call the function.
We can declare the structure pointer and variable inside and outside of the main()
function.
struct structure_name *ptr;
After defining the structure pointer, we need to initialize it, as the code is shown:
ptr = &structure_variable;
There are two ways to access the member of the structure using Structure pointer:
1. Using ( * ) asterisk or indirection operator and dot ( . ) operator.
2. Using arrow ( -> ) operator or membership operator.
3|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi
#include <stdio.h>
// create a structure Subject using the struct keyword
struct Subject
{
// declare the member of the Course structure
char sub_name[30];
int sub_id;
char sub_duration[50];
char sub_type[50];
};
int main()
{
struct Subject sub; // declare the Subject variable
struct Subject *ptr; // create a pointer variable (*ptr)
ptr = ⊂ /* ptr variable pointing to the address of the structure variable sub */
return 0;
o Output:
4|P ag e