C Structures and Unions Assignment Help
Structures
Users can define their own data type using the concept called structure. Structure belongs to user defined data type. Built in data types and user defined data types are used to form User defined type called structure.
Defining structure
struct [structure_tag]
{'//member variable 1'}
{'//member variable 2'}
{'//member variable 3'}
...
[structure_variables];
Example:
struct Student
char name[25];
int age,rollno;
char dept[10];
;
In the above example, Student is the name of the structure is named as tag or structure_tag. name, age, rollno and dept are members are fields.
C Structures and Unions Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com
Declaring structure variables
It can be done in two ways:
- followed by structure definition
- separated from structure definition
Example:
Structure variables followed by structure definition
struct Student
char name[25];
int age,rollno;
char dept[10];
s1,s2..sn; {'//structure variables'}
separated from structure definition
struct Student
char name[25];
int age,rollno;
char dept[10];
;
Student s1,s2…sn;
Accessing Structure Members
Syntax:
Structure_variable(dot operatror)(or)(member access operator)structure_members.
Example:
#include<stdio.h>
#include<string.h>
struct Student
char name[25];
int age,rollno;
char dept[10];
;
int main()
struct Student s1;
printf(“Enter the student details:\n”);
printf(“Enter the name:\n”);
gets(s1.name);
printf(“Enter roll No.:\n”);
scanf(“%d”,&rollno);
printf(“Enter age:\n”);
scanf(“%d”,&age);
printf(“Enter Department:\n”);
gets(s1.dept);
printf(“Student Details are listed below\n:”);
printf("Roll No. of Student 1: %d\n", s1.rollno);
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
printf(“Department: %s\n", s1.name);
return 0;
Output:
Enter the student details:
Enter the name:Ram
Enter roll No.:101
Enter age:18
Enter Department:ECE
Student Details are listed below
Roll No. of Student 1:101
Name of Student 1:Ram
Age of Student 1:18
Department:ECE
Structure Initialization
Structure members can be initialized like any variable.
Example:
struct Student
char name[25];
int age,rollno;
char dept[10];
;
Student s1,s2;
s1.name=”Raju”;
s1.age=20;
s1.rollno=102;
s1.dept=”EEE”;
Array of Structure
From the user defined data type structure any number instance can be created. To create n-number of instances we can use array.
Syntax:
struct structure_tag structure_variable[size_array];
Example:
Student s[10];
Structures as Function Arguments
Function can get structure variable as argument like any other ordinary variable.
Example:
#include<stdio.h>
struct Student
char name[10];
int roll;
float per;
;
void display(struct Student s1);
void main()
struct Student s;
printf("\nEnter Student Details:\n");
printf("\nName:\t");
scanf("%s", s.name);
printf("\nRollno.:\t");
scanf("%d", &s.roll);
printf("\nPercentage:\t");
scanf("%f", &s.per);
show(s);
void show(struct Student s1)
printf("The given inputs are:”);
printf("\nstudent name is %s", s1.name);
printf("\nRoll number is %d", s1.roll);
printf(“\nPercentage is %f”,s1.per);
Pointers to Structures
Structure pointers hold the address of the structure variables. The operator -> used to access the members of the structure using pointer variable.
Syntax:
struct struct_tage *pointer_variable;
Example:
#include <stdio.h>
#include <string.h>
struct student
char name[50];
int rollno;
float per;
;
{'/* function declaration */'}
void display( struct student *s );
int main( )
struct student s1;
printf("\nEnter Student Details:\n");
printf("\nName:\n");
scanf("%s", s1.name);
printf("\nRollno.:\n");
scanf("%d", &s1.rollno);
printf("\nPercentage:\n");
scanf("%f", &s1.per);
{'/* print student info by passing address of s1 */'}
display( &s1 );
return 0;
void display(struct student *s )
Printf(“Given details are…\n”);
printf( "Student Name : %s\n", s->name);
printf( "Roll No. : %d\n", s->rollno);
printf( "Percentage : %f\n", s->per);
Output:
Enter Student Details:
Name:Ram
Rollno.:11
Percentage:80
Given details are…
Student Name:Ram
Roll No.:11
Percentage:80