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

STRUCTURE

The document discusses structures in C programming language. It explains what structures are, how to define and declare structures, initialize structure variables, access structure members, pass structures to functions by value and by reference, create arrays of structures, and nested structures. It also covers unions in C.

Uploaded by

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

STRUCTURE

The document discusses structures in C programming language. It explains what structures are, how to define and declare structures, initialize structure variables, access structure members, pass structures to functions by value and by reference, create arrays of structures, and nested structures. It also covers unions in C.

Uploaded by

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

UNIT II -

STRUCTURES
OUTPUT
#include <stdio.h>
/* Created a structure here. The name of the structure is
* StudentData.
*/
struct StudentData{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
{
/* student is the variable of structure StudentData*/
struct StudentData student;

/*Assigning the values of each struct member here*/


student.stu_name = "Steve";
student.stu_id = 1234;
student.stu_age = 30;

/* Displaying the values of struct members */


printf("Student Name is: %s", student.stu_name);
printf("\nStudent Id is: %d", student.stu_id);
printf("\nStudent Age is: %d", student.stu_age);
return 0;
}
#include <stdio.h>
int main()
{
/* Created a structure here. The name of the structure is
StudentData. */
struct StudentData
{
char *stu_name;
int stu_id;
int stu_age; OUTPUT:
}student; /* student is the variable of structure StudentData*/
/*Assigning the values of each struct member here*/
student.stu_name = "Steve";
student.stu_id = 1234;
student.stu_age = 30;
/* Displaying the values of struct members */
printf("Student Name is: %s", student.stu_name);
printf("\nStudent Id is: %d", student.stu_id);
printf("\nStudent Age is: %d", student.stu_age);
return 0;
}
OUTPUT:
Nested Structures in C
SYNTAX
A structure can be structure tagname_1
{
nested inside another member1;
structure. In other member2;
member3;
words, the members of ...
a structure can be of membern;

any other type including structure tagname_2


{
structure. OUTER member_1;
STRUCTURE INNER
member_2;
STRUCTURE
member_3;
...
member_n;
} var1;

} var2;
EXAMPLE PROGRAM FOR NESTED STRUCTURE
#include<stdio.h>
struct student // OUTER STRUCTURE
{
struct person // INNER STRUCTURE
{
char name[20];
INNER STRUCTURE
int age; MEMBERS
char dob[10];
}info; //INNER STRUCTURE VARIABLE
int roll_no;
float marks; OUTER STRUCTURE
MEMBERS

}s1; // OUTER STRUCTURE VARIABLE


int main() // MAIN CODING OF PROGRAM
{
printf("Details of student: \n\n");
printf("Enter name: ");
scanf("%s", s1.info.name);
printf("Enter age: ");
scanf("%d", &s1.info.age);
printf("Enter dob: ");
scanf("%s", s1.info.dob);
printf("Enter roll no: ");
scanf("%d", &s1.roll_no);
printf("Enter marks: ");
scanf("%f", &s1.marks);
printf("\n*******************************\n\n");

printf("Name: %s\n", s1.info.name);


printf("Age: %d\n", s1.info.age); OUTPUT:
printf("DOB: %s\n", s1.info.dob);
printf("Roll no: %d\n", s1.roll_no);
printf("Marks: %.2f\n", s1.marks);
// signal to operating system program ran fine
return 0;
}
Array of Structures
An array of structure is a group of structure elements
under the same structure variables.
Example: student s1[1000];
The above code creates 1000 elements of structure
type student.
Each element will be structure data type called
student.
The values can be stored into the array of structures as
follows:
s1[0].student_age = 19;
OUTPUT:
PASSING STRUCTURE TO FUNCTION
•A structure can be passed to any function from main
function or from any sub function.
•Structure definition will be available within the function
only.
•It won’t be available to other functions unless it is
passed to those functions by value or by
address(reference).
•Else, we have to declare structure variable as global
variable. That means, structure variable should be
declared outside the main function. So, this structure will
PASSING STRUCTURE TO FUNCTION IN
C:
It can be done in below 3 ways.
1.Passing structure to a function by value
2.Passing structure to a function by
address(reference)
3.No need to pass a structure – Declare
structure variable as global.
EXAMPLE PROGRAM – PASSING STRUCTURE
TO FUNCTION IN C BY VALUE:
 In this program, the whole structure is passed to
another function by value.
 It means the whole structure is passed to another
function with all members and their values.
 So, this structure can be accessed from called
function.
 This concept is very useful while writing very big
programs in C.
PROGRAM TO DISPLAY STUDENT RECORD BY PASSING STRUCTURE TO FUNCTION BY VALUE

#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record);
void main()
{
struct student record;

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

func(record);
}
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000
EXAMPLE PROGRAM – PASSING
STRUCTURE TO FUNCTION IN C BY
ADDRESS:
 In this program, the whole structure is passed
to another function by address.
 It means only the address of the structure is
passed to another function.
 The whole structure is not passed to another
function with all members and their values.
 So, this structure can be accessed from called
function by its address.
EXAMPLE PROGRAM – PASSING STRUCTURE TO FUNCTION IN C BY ADDRESS
#include <stdio.h>
#include <string.h>

struct student
{
int id;
char name[20];
float percentage;
};

void func(struct student *record);

Void main()
{
struct student record;

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

func(&record);
}
void func(struct student *record)
{
printf(" Id is: %d \n", record->id);
printf(" Name is: %s \n", record->name);
printf(" Percentage is: %f \n", record->percentage);
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000
EXAMPLE PROGRAM TO DECLARE A
STRUCTURE VARIABLE AS GLOBAL IN C:
 Structure variables also can be declared as
global variables as we declare other variables in
C.
 So, When a structure variable is declared as
global, then it is visible to all the functions in a
program.
 In this scenario, we don’t need to pass the
structure to any function separately.
#include <stdio.h>
#include <string.h>

struct student
{
int id;
char name[20];
float percentage;
};
struct student record; // Global declaration of structure

void structure_demo();
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
structure_demo();
}
void structure_demo()
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000
UNION
C Union
 Like structure, Union in c language is a user-defined data type that is used to store
the different type of elements.
 At once, only one member of the union can occupy the memory.
 In other words, we can say that the size of the union in any instance is equal to the
size of its largest element.
SYNTAX FOR UNION:

union
union_name
{
data_type member1;
data_type member2;
data_type memeberN;
};
Example program for C union:
#include <stdio.h>
#include <string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
// assigning values to record1 union variable
strcpy(record1.name, "Raju");
strcpy(record1.subject, "Maths");
record1.percentage = 86.50;
printf("Union record1 values example\n");
printf(" Name : %s \n", record1.name);
printf(" Subject : %s \n", record1.subject);
printf(" Percentage : %f \n\n", record1.percent);
// assigning values to record2 union variable
printf("Union record2 values example\n");
strcpy(record2.name, "Mani");
printf(" Name : %s \n", record2.name); Output:
strcpy(record2.subject, "Physics"); Union record1 values example
printf(" Subject : %s \n", record2.subject); Name :
record2.percentage = 99.50; Subject :
printf(" Percentage : %f \n", record2.percentage); Percentage : 86.500000;
return 0; Union record2 values example
} Name : Mani
Subject : Physics
Percentage : 99.500000
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy