0% found this document useful (0 votes)
5 views

abrar (5)

Abrar

Uploaded by

ndass5333
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 views

abrar (5)

Abrar

Uploaded by

ndass5333
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/ 10

MAHATMA GANDHI INSTITUTE OF TECHNOLOGY

Chaitanya Bharathi (P.O.), Gandipet ,Hyderabad

PROGRAMMING FOR PROBLEM SOLVING IN C


Report on
“Structures and intializing of structures in C”

Name:Mirza Abrar Baig


Roll No:24261A05G0
Branch:CSE
Semester:1
Academic Year: 2024-25
Name of the Faculty member:Dr.K.Satish Kumar
Structures:
Definition :
The structure in C is a user-defined data type that can
be used to group related variable(items) of possibly
different types into a single type.

(or)

a user-defined data type that groups related variables


of different types into a single unit.

Important points:
1.The struct keyword is used to define the structure in
the C programming language.

2.Each variable in the structure is known as


a member of the structure.

3.Additionally, the values of a structure are stored in


contiguous memory locations.
Uses:
1. In C, there are cases where we need to store multiple
attributes(variables) of an entity. It is not necessary
that an entity has all the information of one type only.
It can have different attributes of different data types.
For example, an entity Student may have its name
(string), roll number (int), marks (float) so there comes
need of structures.

2. We are often required to work with values of


different data types having certain relationships among
them. For example, a book is described by
its title (string), author (string), price (double), number
of pages (integer), etc.

Difference between structures and strings:


The difference between an array and a structure is that
an array is a homogenous collection of similar types,
whereas a structure can have elements of different
types stored adjacently and identified by a name.
C Structure Declaration:
 We use the struct keyword to declare the structure
and specify its member variables along with their
datatype.
 structure syntax:
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};

Creating structure variables:


1.First way:
struct Person {
char name[50];
int citNo;
float salary;
};
int main() {
struct Person person1, person2, p[20];
return 0; }
(( declaring variables in void rather than in
structure))
2.Second way:
struct Person {
char name[50];
int citNo;
float salary;

} person1, person2, p[20];


((declaring variables at last in structure only))

Basic examples:
#include <stdio.h>
#include <string.h>

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

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

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;
}
Output:
Name: George Orwell
Citizenship No.: 1984
Salary: 2500.00

Memory allocation:
1. Arrays in structure:
 An array of structres in C can be defined as the
collection of multiple structures variables(objects)
where each variable contains information about
different entities.
 The array of structures in C are used to store
information about multiple entities of different
data types.

Declaration:
struct struct_name arr_name [size] = {
{element1_value1, element1_value2, ....},
{element2_value1, element2_value2, ....},
......
......
};
Examples:
#include <stdio.h>
#include <string.h>
struct A {
int var;
};

int main() {

struct A arr[2];

arr[0].var = 10;
arr[1].var = 20;

for (int i = 0; i < 2; i++)


printf("%d\n", arr[i].var);

return 0;
}
Output:
10
20

2.Pointers in structure:
1. Using ( * ) asterisk or indirection operator and dot
( . ) operator.
2. Using arrow ( -> ) operator or membership
operator.

Syntax:
struct struct_name *ptr_name;
Program on pointers in structures:
#include <stdio.h>

struct A {
int var;
};

int main() {
struct A a = {30};

struct A *ptr;
ptr = &a;

printf("%d", ptr->var);

return 0;
}
Output:
30

You might also like