0% found this document useful (0 votes)
13 views4 pages

Unit - 8

PPS UNIT 8

Uploaded by

try.vishal0602
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)
13 views4 pages

Unit - 8

PPS UNIT 8

Uploaded by

try.vishal0602
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
You are on page 1/ 4

PPS – 3110003 Prepared By: Prof.

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.

How to create and declare structure in C


 To create a structure in C, the struct keyword is used followed by the tag name of the
structure. Then the body of the structure is defined, in which the required data members
(primitive or user-defined data types) are added.

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

How to Access Data Members of a Structure Using a Struct Variable


 To create a structure in C, the struct keyword is used followed by the tag name of the
structure. Then the body of the structure is defined, in which the required data members
(primitive or

#include <stdio.h>
#include <string.h>
1|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

// create struct with person1 variable


struct Person {
char name[50];
int citNo;
float salary;
} person1;

int main() {

// assign value to name of person1


strcpy(person1.name, "George Orwell");

// assign values to other person1 variables


person1.citNo = 1984;
person1. salary = 2500;

// print struct variables


printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);

return 0;
}

Array of Structure
#include<stdio.h>
struct Employee
{
char ename[10];
int sal;
};

struct Employee emp[5];


int i, j;
void ask()
{
for(i = 0; i < 3; i++)
{
printf("\nEnter %dst Employee record:\n", i+1);
printf("\nEmployee name:\t");
scanf("%s", emp[i].ename);
printf("\nEnter Salary:\t");
scanf("%d", &emp[i].sal);
}
printf("\nDisplaying Employee record:\n");
for(i = 0; i < 3; i++)
2|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

{
printf("\nEmployee name is %s", emp[i].ename);
printf("\nSlary is %d", emp[i].sal);
}
}
void main()
{
ask();
}

Difference between Structure and Functions

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.

Structures and pointers


 The structure pointer points to the address of a memory block where the Structure is
being stored.
 Structure pointer which tells the address of a structure in memory by pointing pointer
variable to the structure variable.

Declare a Structure Pointer

 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;

Access Structure member using pointer:

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 */

strcpy (sub.sub_name, " Computer Science");


sub.sub_id = 1201;
strcpy (sub.sub_duration, "6 Months");
strcpy (sub.sub_type, " Multiple Choice Question");

// print the details of the Subject;


printf (" Subject Name: %s\t ", (*ptr).sub_name);
printf (" \n Subject Id: %d\t ", (*ptr).sub_id);
printf (" \n Duration of the Subject: %s\t ", (*ptr).sub_duration);
printf (" \n Type of the Subject: %s\t ", (*ptr).sub_type);

return 0;

o Output:

Subject Name: Computer Science


Subject Id: 1201
Duration of the Subject: 6 Months
Type of the Subject: Multiple Choice Question

4|P ag e

You might also like